在工作中,遇到verdi的License不够的情况,某些人占用了多个License,为及时获得一个可用的License,写了一个perl来kill运行时间最长的进程。
主要功能
- 在perl中,通过(lmstat -a)或者(ps -ef)得到verdi运行情况
- 得到verdi分配情况
- 得到verdi占用最多的用户的使用信息
- 活动该用户使用最长的verdi进程号
具体执行情况
方法一:推荐
方法二:
Perl代码
方法一:推荐
#!/usr/bin/perl
#----------------------------------------------------------------------
#
# Filename: start_verdi
# Description: file function description
#
# Author: 乔木
# Version: 1.0.0
# Create: 2017-12-05 21:29:05
# Last Modified: 2017-12-05 21:29:05
# History: Modify the history
#----------------------------------------------------------------------
#####################################################
# start_verdi
#####################################################
use warnings;
#####################################################
my (@license_stat,$license_flag,@users_license,@users_name,%verdi_user,$mostverdi_user,$total_lic,$use_lic);
@license_stat = `lmstat -f Verdi`;
#####################################################
# get license info
#####################################################
$license_flag = 0;
foreach my $line (@license_stat){
chomp($line);
next if($line =~ /^\s*$/);
if($line =~ /Users of Verdi/){
$line =~ /Total of (\d+) licenses issued; Total of (\d+) licenses in use/;
$total_lic = $1;
$use_lic = $2;
#print("total_lic = $total_lic, use_lic = $use_lic\n");
if($use_lic < $total_lic){
system("verdi @ARGV");
exit;
}
next;
}
if($line =~ /^-----------/){
$license_flag = 0;
#print("$line\n");
last;
}
if($license_flag){
push(@users_license, $line);
}
if($line =~ /"Verdi"/){
$license_flag = 1;
#print("$line\n");
}
}
#####################################################
# handle info
#####################################################
shift(@users_license); #delect first line
foreach my $line (@users_license){
chomp($line);
#print("$line\n");
push(@users_name,(split /\s+/, $line)[1]);
}
#print("users_name = @users_name\n");
#####################################################
# creat user hash >> verdi number
#####################################################
foreach my $user (@users_name){
$verdi_user{$user} += 1;
}
my $user_num= keys %verdi_user;
my $i=0;
foreach (sort {$verdi_user{$a} <=> $verdi_user{$b}} keys %verdi_user) {
$i++;
#printf("%-15s %d verdi\n",$_,$verdi_user{$_} );
if($i == $user_num){
$mostverdi_user = $_;
print("\n$_ has the most verdi: $verdi_user{$_}\n\n");
}
}
#####################################################
# get process info
#####################################################
foreach my $line (@users_license){
chomp($line);
#print("$line\n");
my $user_name = (split /\s+/, $line)[1];
if($user_name eq $mostverdi_user){
#print("$line\n");
$line =~ /\((mm\d.*)\)/;
my $user_process = $1;
#print("$user_process\n");
$user_process =~ s/\// /;
#print("lmremove -h Verdi $user_process\n");
system("lmremove -h Verdi $user_process");
system("verdi @ARGV");
last;
}
}
exit;
方法二:
#!/usr/bin/perl
#----------------------------------------------------------------------
#
# Filename: reqverdi
# Description: file function description
#
# Author:
# Version: 1.0.0
# Create: 2017-12-01 07:23:56
# Last Modified: 2017-12-01 07:23:56
# History: Modify the history
#----------------------------------------------------------------------
#####################################################
# reqverdi
#####################################################
use warnings;
my (%verdi_user,$mostverdi_user,%prosess_hash,$longest_prosess);
#####################################################
# 获得verdi进程信息
#####################################################
my @user_info = `ps -ef | grep Novas | grep -v "grep" | awk '{print \$1}'`;
for(my $i=0;$i < @user_info;$i++){
chomp($user_info[$i]);
#print("$user_info[$i]\n");
}
#####################################################
# creat user hash >> verdi number verdi分配情况
#####################################################
foreach my $user (@user_info){
$verdi_user{$user} += 1;
}
my $user_num= keys %verdi_user;
my $i=0;
foreach (sort {$verdi_user{$a} <=> $verdi_user{$b}} keys %verdi_user) {
$i++;
printf("%-15s %d verdi\n",$_,$verdi_user{$_} );
if($i == $user_num){
$mostverdi_user = $_;
print("\n$_ has the most verdi: $verdi_user{$_}\n\n");
}
}
#####################################################
# get $mostverdi_user info 获得占用最多verdi用户的verdi运行时间
#####################################################
my @most_info = `ps -ef | grep Novas| grep $mostverdi_user| grep -v "grep"`;
#print "@most_info\n";
print("user prosess timer\n");
foreach my $process (@most_info){
my @clump = split /\s+/,$process;
printf("%-10s %-10s %s\n",$clump[0],$clump[1],$clump[6]);
############################
# convert timer
############################
my @day_timer = split /-/,$clump[6];
my @timer = split /:/,$day_timer[-1];
#print("@day_timer @timer\n");
my $timer_convt = 0;
if(@day_timer == 2){
$timer_convt = $day_timer[0]*24*60+$timer[0]*60+$timer[1];
#print("$timer_convt\n");
}
else{
$timer_convt = $timer[0]*60+$timer[1];
#print("$timer_convt\n");
}
$prosess_hash{$clump[1]} = $timer_convt;
}
#####################################################
# get $mostverdi_user the longest run timer prosess number
# 得到运行最长的进程号
#####################################################
my $prosess_num= keys %prosess_hash;
$i=0;
foreach (sort {$prosess_hash{$a} <=> $prosess_hash{$b}} keys %prosess_hash) {
$i++;
#printf("%-15s %s\n",$_,$prosess_hash{$_} );
if($i == $prosess_num){
$longest_prosess = $_;
my $days = int($prosess_hash{$_}/(24*60));
my $hours = int($prosess_hash{$_}%(24*60)/60);
print("\n$_ has run $days day $hours hours\n\n");
}
}
print("kill -9 $longest_prosess\n");
system"kill -9 $longest_prosess";
|
请发表评论