Guest User

Untitled

a guest
Jul 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. ###############################################################################
  4. # vzstats.pl
  5. #
  6. # this script reads /proc/user_beancounters on openvz HNs and VEs and displays
  7. # the values in human-readable format (megabytes/kilobytes).
  8. #
  9. # The script can be distributed freely for everybody who finds it usable.
  10. #
  11. # Christian Anton <mail |_at_| christiananton.de> 2008-09-18
  12.  
  13.  
  14.  
  15. open(BEANS,"/proc/user_beancounters");
  16. chomp ($arch = `uname -m`);
  17.  
  18. sub check_maxulong {
  19. my $number = shift;
  20.  
  21. if ($arch eq "x86_64") {
  22. if ($number == 9223372036854775807) {
  23. return 1;
  24. } else {
  25. return undef;
  26. }
  27. } else {
  28. if ($number == 2147483647) {
  29. return 1;
  30. } else {
  31. return undef;
  32. }
  33. }
  34. }
  35.  
  36. sub recalc_bytes {
  37. my $bytes = shift;
  38.  
  39. if (defined(&check_maxulong($bytes))) { return "MAX_ULONG"; }
  40.  
  41. my $kbytes = $bytes / 1024;
  42. my $ret;
  43.  
  44. # if over 1mb, show mb values
  45. if ($kbytes > 1024) {
  46. my $mbytes = $kbytes / 1024;
  47. $ret = sprintf("%.2f", $mbytes) . " mb";
  48. return $ret;
  49. } else {
  50. $ret = sprintf("%.2f", $kbytes) . " kb";
  51. return $ret;
  52. }
  53. }
  54.  
  55. sub recalc_pages {
  56. my $pages = shift;
  57.  
  58. if ($pages == 0) { return "0"; }
  59. if (defined(&check_maxulong($pages))) { return "MAX_ULONG"; }
  60.  
  61. my $kbytes = $pages * 4;
  62. my $ret;
  63.  
  64. if ($kbytes > 1024) {
  65. my $mbytes = $kbytes / 1024;
  66. $ret = sprintf("%.2f", $mbytes) . " mb";
  67. return $ret;
  68. } else {
  69. $ret = sprintf("%.2f", $kbytes) . " kb";
  70. return $ret;
  71. }
  72. }
  73.  
  74. sub recalc_nothing {
  75. my $number = shift;
  76. if (defined(&check_maxulong($number))) { return "MAX_ULONG"; }
  77.  
  78. return $number;
  79. }
  80.  
  81. sub printline {
  82. my $mode = shift; # 0=normal, 1=bytes, 2=pages
  83. my $ident = shift;
  84. my $held = shift;
  85. my $maxheld = shift;
  86. my $barrier = shift;
  87. my $limit = shift;
  88. my $failcnt = shift;
  89.  
  90. if ($mode == 0) {
  91. printf ("%-15s",$ident);
  92. printf ("%18s",&recalc_nothing($held));
  93. printf ("%21s",&recalc_nothing($maxheld));
  94. printf ("%21s",&recalc_nothing($barrier));
  95. printf ("%21s",&recalc_nothing($limit));
  96. printf ("%21s",$failcnt);
  97. print "\n";
  98. } elsif ($mode == 1) {
  99. printf ("%-15s",$ident);
  100. printf ("%18s",&recalc_bytes($held));
  101. printf ("%21s",&recalc_bytes($maxheld));
  102. printf ("%21s",&recalc_bytes($barrier));
  103. printf ("%21s",&recalc_bytes($limit));
  104. printf ("%21s",$failcnt);
  105. print "\n";
  106. } elsif ($mode == 2) {
  107. printf ("%-15s",$ident);
  108. printf ("%18s",&recalc_pages($held));
  109. printf ("%21s",&recalc_pages($maxheld));
  110. printf ("%21s",&recalc_pages($barrier));
  111. printf ("%21s",&recalc_pages($limit));
  112. printf ("%21s",$failcnt);
  113. print "\n";
  114. }
  115. }
  116.  
  117. sub work_line {
  118. my $line = shift;
  119. my $ident = $line;
  120. my $held = $line;
  121. my $maxheld = $line;
  122. my $barrier = $line;
  123. my $limit = $line;
  124. my $failcnt = $line;
  125.  
  126.  
  127.  
  128. $ident =~ s/^\s+(\w+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$/$1/;
  129. $held =~ s/^\s+(\w+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$/$2/;
  130. $maxheld =~ s/^\s+(\w+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$/$3/;
  131. $barrier =~ s/^\s+(\w+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$/$4/;
  132. $limit =~ s/^\s+(\w+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$/$5/;
  133. $failcnt =~ s/^\s+(\w+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$/$6/;
  134.  
  135. # 0=normal, 1=bytes, 2=pages
  136. if ($ident eq "dummy") {
  137. # do nothing, skip this line
  138. } elsif ($ident =~ /pages/) {
  139. &printline(2,$ident,$held,$maxheld,$barrier,$limit,$failcnt);
  140. } elsif ($ident =~ /^num/) {
  141. &printline(0,$ident,$held,$maxheld,$barrier,$limit,$failcnt);
  142. } else {
  143. &printline(1,$ident,$held,$maxheld,$barrier,$limit,$failcnt);
  144. }
  145.  
  146. }
  147.  
  148. sub print_header {
  149. my $uid = shift;
  150.  
  151. print "#####################################################################################################################\n";
  152. print "BEANS FOR UID $uid\n";
  153. print "resource held maxheld barrier limit failcnt\n";
  154. }
  155.  
  156. # now eat your beans baby
  157. while (<BEANS>) {
  158. chomp($line = $_);
  159.  
  160. # skip processing of headline
  161. if ($line =~ /^\s+uid/) {
  162. # do nothing, skip this
  163. } elsif ($line =~ /^Ver/) {
  164. # do nothing, skip this
  165. } elsif ($line =~ /^\s+\d+:\s+kmem/) {
  166. $uid = $line;
  167. $line =~ s/^(\s+)(\d+):/$1/;
  168. $uid =~ s/^(\s+)(\d+):.*$/$2/;
  169. &print_header($uid);
  170. &work_line($line);
  171. } else {
  172. &work_line($line);
  173. }
  174. }
  175.  
  176. close(BEANS);
Add Comment
Please, Sign In to add comment