• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Perl自动释放Licence启动Verdi

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

在工作中,遇到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";

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
perl的map函数发布时间:2022-07-22
下一篇:
perl之创建临时文件夹遇到同名文件该咋办发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap