Allegato "top_stat.pl"
Scarica 1 #!/usr/bin/perl
2
3 die "Troppi parametri in input" if($#ARGV > 0);
4 my $OPTION = shift || "-c";
5 die "Argomento $OPTION non valido, provare con -c o -m \n" if ($OPTION ne "-c" && $OPTION ne "-m");
6
7 my $column_index = 8;
8 $column_index = 9 if ($OPTION eq "-m");
9
10 my @file = qx{top -n 1 -b | tail -n +7};
11
12 # Ordinare in maniera decrescente gli utenti che usano più memoria/cpu
13 my %utentiConsumi_hash;
14
15 for(@file) {
16 @colonne = split(' ', $_);
17 if($colonne[1] =~ m/USER/)
18 {
19 print "$colonne[1] --- $colonne[$column_index]\n";
20 open($log_file, ">>", "top_stat.log") || die ("Cannot open file !");
21 print $log_file "$colonne[1] --- $colonne[$column_index]\n";
22 close $log_file;
23 next;
24 }
25 $utentiConsumi_hash{$colonne[1]} += $colonne[$column_index];
26 }
27
28 # Ordinamento hash
29 open($log_file, ">>", "top_stat.log") || die ("Cannot open file !");
30 foreach $key ( sort { ($utentiConsumi_hash{$b} <=> $utentiConsumi_hash{$a}) || ($a cmp $b)} keys %utentiConsumi_hash ) {
31 print "$key --- $utentiConsumi_hash{$key}% \n";
32 print $log_file "$key --- $utentiConsumi_hash{$key}% \n";
33 }
34
35 print $log_file "#"x20;
36 print $log_file "\n";
37 close $log_file;
Allegati
Non è consentito inserire allegati su questa pagina.