I’m too lazy to open yet another window for documentation on where your server is at.
Ghetto Xen Status Report
February 20th, 2010
Pointlessly long script to give a report on total/available memory and a list of domU’s, their memory allocation, and number of vcpus on a Xen server.
It provides output similar to the following:
Status report as of: 2010-02-19 09:00:01 dom0 Information: ------------------------------------------------------------------------------ host lolhost.irtehrox.com total_memory 65535 free_memory 9276 Running domU's: ------------------------------------------------------------------------------ NAME: MEMORY: VCPU: 20000-web002 2048 1 20001-web001 12288 4 20001-web002 12288 4 50005-db002 2048 1 50005-web002 4096 1 60015-dev001 256 1 60015-web004 4096 1 80123-web005 8192 1 80123-web006 8192 1
While you certainly could glean this information by looking at the list of systems you should be keeping, or even logging into the server itself, who has time for that? :p
The good stuff:
#!/usr/bin/perl # Configuration ------------------------------------------------------------- # Domain to send e-mail from; for the actual user, we use our hostname my $SENDER = "somedomain.foo"; # Address to e-mail report to my $RECEIVER = 'user@somehost.foo'; # --------------------------------------------------------------------------- # Global Variables my $hostname = "unknown"; my $email_body = ""; my $stamp = "unknown"; my $hr = "------------------------------------------------------------------------------"; # Part 1: Intial setup; grab our time, hostname, etc. open(HOSTNAME, "/bin/hostname |"); while() { # Get rid of the break chomp($_); # Kill anything after a proper hostname; this may need to be commented out # or modified depending on your needs $_ =~ s/\..*//g; $hostname = $_; } close(HOSTNAME); $stamp = qx(date +"%Y-%m-%d %H:%M:%S"); chomp($stamp); $email_body = $email_body . "Status report as of: $stamp\n"; # Part 2: Grab key Xen info for the dom0 my (@info_holder); $email_body = $email_body . "\n\ndom0 Information:\n$hr\n\n"; open(XM, "/usr/sbin/xm info |"); while( ) { # Get rid of the break chomp($_); # Selective acquisition of data if(($_ =~ /^host/) || ($_ =~ /^total_memory/) || ($_ =~ /^free_memory/)) { # Quick regexp to ensure the line can be split easily $_ =~ s/\s+/ /g; # Eliminate the colon $_ =~ s/: //g; # Split our line @info_holder = split(/ /, $_); # Attach line to e-mail $email_body = $email_body . sprintf("%-20s %s", $info_holder[0], $info_holder[1]) . "\n"; } } close(XM); # Part 3: Grab list of running domU's my (@domu_holder, @domu_out, $i); $i = 0; open(XM, "/usr/sbin/xm list |"); while( ) { # Get rid of the break chomp($_); # Kill the header and Dom-0 lines if(($_ =~ /^Name /) || ($_ =~ /^Domain-0 /)) { next; } # Quick regexp to ensure the line can be split easily. $_ =~ s/\s+/ /g; # Insert into our holding array $domu_holder[$i] = $_; # Increment our counter $i++; } close XM; @domu_holder = sort(@domu_holder); $email_body = $email_body . "\n\nRunning domU's:\n$hr\n\nNAME: MEMORY: VCPU:\n"; foreach(@domu_holder) { # Split the line so we can extract name, vcpu, mem @domu_out = split(' ', $_); # Format it up! $email_body = $email_body . sprintf("%-20s %7s %5s", $domu_out[0], $domu_out[2], $domu_out[3]) . "\n"; } # Finally, send the e-mail open(SENDMAIL, "|/usr/sbin/sendmail -t"); print SENDMAIL "To: $RECEIVER\n"; print SENDMAIL "From: $hostname\@$SENDER\n"; print SENDMAIL "Subject: Xen Status Report For $stamp\n"; print SENDMAIL $email_body . "\n"; close(SENDMAIL);
No doubt there’s an easier way to go about doing this, but I have a talent for simplifying the complex and complicating the obvious. Should’ve been a politician.
Note: Still working out comments/etc. theming. Please ignore the ugliness.