disk-stats.pl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 30 January 2013
  5. # https://github.com/trizen
  6. # Show disk and RAM usage.
  7. use 5.010;
  8. use strict;
  9. use warnings;
  10. use List::Util qw(max);
  11. use Term::ANSIColor qw(colored color);
  12. use Number::Bytes::Human qw(format_bytes);
  13. my %CONFIG = (DF_COMMAND => 'df -Th');
  14. sub get_ram {
  15. # RAM
  16. my $freeram = 0;
  17. my $totalram = 0;
  18. my $match_ram = qr/:\s+(\d+)/;
  19. {
  20. open my $ram_fh, '<', '/proc/meminfo';
  21. while (defined(my $ram_line = <$ram_fh>)) {
  22. $totalram = $1 / 1024 if $. == 1 and $ram_line =~ /$match_ram/o;
  23. $freeram += $1 / 1024 if $. > 1 and $ram_line =~ /$match_ram/o;
  24. last if $. == 4;
  25. }
  26. close $ram_fh;
  27. }
  28. my $usedram = $totalram - $freeram;
  29. my $used_percent = $usedram / $totalram * 100;
  30. return
  31. scalar {
  32. name => "/dev/mem",
  33. used => format_bytes($usedram * 1024**2),
  34. total => format_bytes($totalram * 1024**2),
  35. used_percent => $used_percent,
  36. };
  37. }
  38. sub get_partitions {
  39. my @partitions;
  40. open my $df_pipe, '-|', $CONFIG{DF_COMMAND};
  41. while (defined($df_pipe) and defined(my $line = <$df_pipe>)) {
  42. chomp($line);
  43. my (undef, $type, $totalsize, $used, undef, $used_percent, $mountpoint) = split(' ', $line, 7);
  44. $used_percent =~ s/^\d+\K%\z// or next;
  45. #$mountpoint =
  46. # $mountpoint eq '/' ? 'Root'
  47. # : $mountpoint =~ m{^.*/}s ? ucfirst substr($mountpoint, $+[0])
  48. # : ucfirst $mountpoint;
  49. push @partitions,
  50. scalar {
  51. name => $mountpoint,
  52. used_percent => $used_percent,
  53. total => $totalsize,
  54. used => $used,
  55. };
  56. }
  57. close $df_pipe;
  58. my %seen;
  59. return grep { !$seen{join $;, %{$_}}++ } @partitions;
  60. }
  61. my @data = (get_ram(), get_partitions());
  62. my %data;
  63. push @{$data{names}}, map { $_->{name} } @data;
  64. push @{$data{usage}}, map { "$_->{used}/$_->{total}" } @data;
  65. my $left_cut = max(map { length } @{$data{names}});
  66. my $right_cut = max(map { length } @{$data{usage}});
  67. my $width = (split(' ', `stty size`))[1];
  68. foreach my $i (0 .. $#data) {
  69. my $hash_ref = $data[$i];
  70. my $barw = $width - ($left_cut + $right_cut + 2);
  71. my $used = sprintf "%.0f", $barw * ($hash_ref->{used_percent} / 100);
  72. my $bar = '';
  73. my $pos = 0;
  74. my $bleft = 0;
  75. my @colors = ([50, 'green'], [80, 'yellow'], [100, 'red']);
  76. until ($bleft >= $used) {
  77. my ($size, $color) = @{shift @colors};
  78. my $barsize = sprintf "%.0f",
  79. $hash_ref->{used_percent} > $size ? (($size - $pos) / 100 * $barw) : ($used - $bleft);
  80. $bar .= colored('>' x $barsize, "bold $color");
  81. $pos += $size;
  82. $bleft += $barsize;
  83. }
  84. printf "%s%-${left_cut}s%s[%s%s]%s%${right_cut}s%s\n", color('bright_blue'), $data{names}[$i], color('reset'),
  85. $bar, " " x ($barw - $used), color('green'), $data{usage}[$i], color('reset');
  86. }