Guest User

Untitled

a guest
Jul 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. use Getopt::Std;
  3.  
  4. # Get commandline options
  5. # -v {VEID} optional VEID for writing into VE
  6. # -f {FILENAME} optional Filename in- or outside VE
  7. # -r bool optional Replace current statsfile, if exists
  8.  
  9. getopts('rv:f:');
  10. if ($opt_v && !$opt_f) {
  11. print "Cannot write to VE (-v) without filename (-f)\nUSAGE: $0 --help\n";
  12. exit(1);
  13. }
  14.  
  15. if (!$opt_f) {
  16. $filename = "stats.xml";
  17. } else {
  18. if ($opt_v) {
  19. $filename = "/var/lib/vz/private/$opt_v";
  20. }
  21. $filename .= $opt_f;
  22. }
  23.  
  24. # If -r option is set, set fileopen modifier to ">" instead of ">>"
  25.  
  26. if ($opt_r) {
  27. $fmod = ">";
  28. } else {
  29. $fmod = ">>";
  30. }
  31.  
  32. print "Writing stats to $filename\n";
  33.  
  34. # ---------------------------------------
  35. # Get vzlist and isolate Ids and Procnums
  36.  
  37. @lines = split(/\n/, `vzlist -a`);
  38.  
  39. foreach (@lines) {
  40. if ($_ =~ /^[\s]+([0-9]+)[\s]+([0-9]+)[\s]+running/) {
  41. push(@ids, $1);
  42. push(@procs, $2);
  43. }
  44. }
  45.  
  46. # Write maintag, time and number of VEs running
  47.  
  48. if (!open(LOG, $fmod.$filename)) {
  49. print "Couldn't open File. Fail.\n";
  50. exit(1);
  51. }
  52.  
  53. print LOG "<stats>\n\t<time>".(time())."</time>\n";
  54. print LOG "\t<running>".($#ids+1)."</running>\n";
  55.  
  56. # Write tag for hostnode
  57. # Fill in different memory values
  58.  
  59. $mem = `free -m -o`;
  60. if ($mem =~ /Mem:[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+[0-9]+[\s]+[0-9]+[\s]+([0-9]+)/) {
  61. print LOG "\t<hn>\n";
  62. print LOG "\t\t<total>".($1)."</total>\n";
  63. print LOG "\t\t<used>".($2)."</used>\n";
  64. print LOG "\t\t<free>".($3)."</free>\n";
  65. print LOG "\t\t<cached>".($4)."</cached>\n";
  66.  
  67. }
  68.  
  69. # Get hostnode loadaverage (5m)
  70. # and write
  71.  
  72. $cpu = `uptime`;
  73. if ($cpu =~ /load\saverage:\s.*,\s([0-9]\.[0-9]{2})/) {
  74. print LOG "\t\t<load>".($1)."</load>\n";
  75. }
  76.  
  77. print LOG "\t</hn>\n";
  78.  
  79. # Now close hostnode and go to VEs
  80. # Set total used memory by all VEs to 0
  81. # Write tags for each VE and fill in different memory values
  82.  
  83. $allmem = 0;
  84. $i = 0;
  85. foreach (@ids) {
  86. $mem = `vzctl exec $_ free -m -o`;
  87. if ($mem =~ /Mem:[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+[0-9]+[\s]+[0-9]+[\s]+([0-9]+)/) {
  88. print LOG "\t<ve>\n";
  89. print LOG "\t\t<id>".($_)."</id>\n";
  90. print LOG "\t\t<total>".($1)."</total>\n";
  91. print LOG "\t\t<used>".($2)."</used>\n";
  92. print LOG "\t\t<free>".($3)."</free>\n";
  93. print LOG "\t\t<cached>".($4)."</cached>\n";
  94. print LOG "\t\t<procs>".($procs[$i])."</procs>\n";
  95. print LOG "\t</ve>\n";
  96. $i++;
  97. $allmem += $2;
  98. }
  99. }
  100.  
  101. # Write overall mem occupied by all VEs
  102.  
  103. print LOG "\t<allused>".($allmem)."</allused>\n";
  104. print LOG "</stats>";
  105. close(LOG);
Add Comment
Please, Sign In to add comment