Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- use Getopt::Std;
- # Get commandline options
- # -v {VEID} optional VEID for writing into VE
- # -f {FILENAME} optional Filename in- or outside VE
- # -r bool optional Replace current statsfile, if exists
- getopts('rv:f:');
- if ($opt_v && !$opt_f) {
- print "Cannot write to VE (-v) without filename (-f)\nUSAGE: $0 --help\n";
- exit(1);
- }
- if (!$opt_f) {
- $filename = "stats.xml";
- } else {
- if ($opt_v) {
- $filename = "/var/lib/vz/private/$opt_v";
- }
- $filename .= $opt_f;
- }
- # If -r option is set, set fileopen modifier to ">" instead of ">>"
- if ($opt_r) {
- $fmod = ">";
- } else {
- $fmod = ">>";
- }
- print "Writing stats to $filename\n";
- # ---------------------------------------
- # Get vzlist and isolate Ids and Procnums
- @lines = split(/\n/, `vzlist -a`);
- foreach (@lines) {
- if ($_ =~ /^[\s]+([0-9]+)[\s]+([0-9]+)[\s]+running/) {
- push(@ids, $1);
- push(@procs, $2);
- }
- }
- # Write maintag, time and number of VEs running
- if (!open(LOG, $fmod.$filename)) {
- print "Couldn't open File. Fail.\n";
- exit(1);
- }
- print LOG "<stats>\n\t<time>".(time())."</time>\n";
- print LOG "\t<running>".($#ids+1)."</running>\n";
- # Write tag for hostnode
- # Fill in different memory values
- $mem = `free -m -o`;
- if ($mem =~ /Mem:[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+[0-9]+[\s]+[0-9]+[\s]+([0-9]+)/) {
- print LOG "\t<hn>\n";
- print LOG "\t\t<total>".($1)."</total>\n";
- print LOG "\t\t<used>".($2)."</used>\n";
- print LOG "\t\t<free>".($3)."</free>\n";
- print LOG "\t\t<cached>".($4)."</cached>\n";
- }
- # Get hostnode loadaverage (5m)
- # and write
- $cpu = `uptime`;
- if ($cpu =~ /load\saverage:\s.*,\s([0-9]\.[0-9]{2})/) {
- print LOG "\t\t<load>".($1)."</load>\n";
- }
- print LOG "\t</hn>\n";
- # Now close hostnode and go to VEs
- # Set total used memory by all VEs to 0
- # Write tags for each VE and fill in different memory values
- $allmem = 0;
- $i = 0;
- foreach (@ids) {
- $mem = `vzctl exec $_ free -m -o`;
- if ($mem =~ /Mem:[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+[0-9]+[\s]+[0-9]+[\s]+([0-9]+)/) {
- print LOG "\t<ve>\n";
- print LOG "\t\t<id>".($_)."</id>\n";
- print LOG "\t\t<total>".($1)."</total>\n";
- print LOG "\t\t<used>".($2)."</used>\n";
- print LOG "\t\t<free>".($3)."</free>\n";
- print LOG "\t\t<cached>".($4)."</cached>\n";
- print LOG "\t\t<procs>".($procs[$i])."</procs>\n";
- print LOG "\t</ve>\n";
- $i++;
- $allmem += $2;
- }
- }
- # Write overall mem occupied by all VEs
- print LOG "\t<allused>".($allmem)."</allused>\n";
- print LOG "</stats>";
- close(LOG);
Add Comment
Please, Sign In to add comment