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\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";

最新文章

  1. 安装ArcGIS Engine 9.3
  2. Moon.Orm性能报告
  3. XmlException: 名称不能以“&lt;”字符(十六进制值 0x3C)开头
  4. idea中配置eslint 静态代码检查
  5. 清北学堂2017NOIP冬令营入学测试P4749 F’s problem(f)
  6. 导航栏视图设置 tabbleView 是设置总背景图
  7. Oracle 分析函数之聚集函数(MAX、MIN、AVG和SUM)
  8. ZOJ 2724 Windows 消息队列 (优先队列)
  9. ionic打包项目,运行时报错A problem occurred configuring root project &#39;android&#39;。。。
  10. 一些LVS实验配置、工具和方案
  11. 针对Student表的DAO设计实例
  12. MySQL 查询重复数据,删除重复数据保留id最小的一条作为唯一数据
  13. python2.7安装
  14. bash内置命令mapfile:读取文件内容到数组
  15. Linux常用基本命令:三剑客命令之-awk 三元表达式
  16. Mac下git通过SSH进行免密码安全连接github
  17. golang 如何查看channel通道中未读数据的长度
  18. python文件操作 二
  19. C++获取当前进程绝对路径
  20. notepad++ jstool 插件安装

热门文章

  1. 用for和while循环求e的值[e=1+1/1!+1/2!+1/3!+1/4!+1/5!+...+1/n!]
  2. open阶段的一致性检验(二)
  3. 4个开源的Gmail替代品
  4. python学习一,python基本语法
  5. CList 点击表头排序 (2)两种排序方法中其中一种
  6. python 命令行參数解析
  7. 一起talk C栗子吧(第九回:C语言实例--最大公约数)
  8. Android应用开发-广播和服务
  9. web service 原理
  10. [appium]-9宫格解锁方法