zoukankan      html  css  js  c++  java
  • Perl自动释放Licence启动Verdi

    Perl自动释放Licence启动Verdi

    在工作中,遇到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
    ");
            if($use_lic < $total_lic){
                system("verdi @ARGV");
                exit;
            }
            next;
        }
        if($line =~ /^-----------/){
            $license_flag = 0;
            #print("$line
    ");
    
           last;
        }
        if($license_flag){
            push(@users_license, $line);
        }
        if($line =~ /"Verdi"/){
            $license_flag = 1;
            #print("$line
    ");
        }
    }
    
    #####################################################
    #  handle info
    #####################################################
    shift(@users_license);  #delect first line
    foreach my $line (@users_license){
        chomp($line);
        #print("$line
    ");
        push(@users_name,(split /s+/, $line)[1]);
    }
    
    #print("users_name = @users_name
    ");
    
    #####################################################
    #  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
    ",$_,$verdi_user{$_} );
        if($i == $user_num){
            $mostverdi_user = $_;
            print("
    $_ has the most verdi: $verdi_user{$_}
    
    ");
        }
    }
    
    #####################################################
    #  get process info
    #####################################################
    foreach my $line (@users_license){
        chomp($line);
        #print("$line
    ");
        my $user_name = (split /s+/, $line)[1];
    
        if($user_name eq $mostverdi_user){
            #print("$line
    ");
            $line =~ /((mmd.*))/;
            my $user_process = $1;
            #print("$user_process
    ");
            
            $user_process =~ s/// /;
            #print("lmremove -h Verdi $user_process
    ");
            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]
    ");
    }
    
    #####################################################
    #  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
    ",$_,$verdi_user{$_} );
        if($i == $user_num){
            $mostverdi_user = $_;
            print("
    $_ has the most verdi: $verdi_user{$_}
    
    ");
        }
    }
    
    #####################################################
    #  get $mostverdi_user info 获得占用最多verdi用户的verdi运行时间
    #####################################################
    my @most_info  = `ps -ef | grep Novas| grep $mostverdi_user| grep -v "grep"`;
    #print "@most_info
    ";
    
    print("user       prosess    timer
    ");
    foreach my $process (@most_info){
         my @clump = split /s+/,$process;
         printf("%-10s %-10s %s
    ",$clump[0],$clump[1],$clump[6]);
    
         ############################
         # convert timer
         ############################
         my @day_timer = split /-/,$clump[6];
         my @timer = split /:/,$day_timer[-1];
         #print("@day_timer  @timer
    ");
    
         my $timer_convt = 0;
         if(@day_timer == 2){
             $timer_convt = $day_timer[0]*24*60+$timer[0]*60+$timer[1];
             #print("$timer_convt
    ");
         }
         else{
             $timer_convt = $timer[0]*60+$timer[1];
             #print("$timer_convt
    ");
         }
    
         $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
    ",$_,$prosess_hash{$_} );
        if($i == $prosess_num){
            $longest_prosess = $_;
            my $days    = int($prosess_hash{$_}/(24*60));
            my $hours = int($prosess_hash{$_}%(24*60)/60);
            print("
    $_ has run $days day $hours hours
    
    ");
        }
    }
    
    print("kill -9 $longest_prosess
    ");
    system"kill -9 $longest_prosess";
    
  • 相关阅读:
    Apache虚拟主机(VirtualHost)配置
    LAMP源码安装
    SUSE上配置SAMBA服务
    Linux下安装或升级Python 2.7
    HTML5,CSS3,JS绘制饼图
    Single Number
    Reverse Words in a String
    C++控制台日历
    百度JS破盗链
    腾讯前端突击队Ⅱ
  • 原文地址:https://www.cnblogs.com/OneFri/p/7940831.html
Copyright © 2011-2022 走看看