#!/usr/bin/env perl # # @(#) xsysstats_wrapper v2.2.0 a xsysstats wrapper program # # # Copyright 2009-2010 Jim Pirzyk / pirzyk.org # All rights reserved. # use Getopt::Std; use strict; use warnings; no warnings 'once'; # The program defaults my %graphs = ( 'cpu' => { Color => 'red', DarkColor => 'DarkRed', }, 'load1' => { Color => 'orange', DarkColor => 'DarkOrange', }, 'disk' => { Color => 'green', DarkColor => 'DarkGreen', }, 'packets' => { Color => 'yellow', DarkColor => 'GoldenRod', }, # 'swap' => { # Color => 'white', # DarkColor => 'black', # }, # 'pagei' => { # Color => 'purple', # DarkColor => 'purple', # }, # 'interrupts' => { # Color => 'brown', # DarkColor => 'brown', # }, ); my (@hosts, %opts); sub get_size { my ($resolution) = @_; my ($x, $y) = split (/x/, $resolution); my ($size) = int ($x / 16); return ($size > 66 ? $size: 66); } sub cal_coord { my ($gcnt, $hcnt, $gsize) = @_; my ($res); # Since we have so few graphs, have each graph be 4x1 or 2x1 instead of 1x1 if ( defined $gsize ) { if ($gcnt <= 2) { $gcnt *= 4; } elsif ($gcnt <= 4) { $gcnt *= 2; } } else { $gsize=1; } if ( $opts{'p'} ) { $res = ($hcnt*$gsize*$opts{'c'}) . 'x' . (int($gcnt/$opts{'c'}+0.5)*$gsize); } else { $res = ($gcnt*$gsize*$opts{'c'}) . 'x' . (int($hcnt/$opts{'c'}+0.5)*$gsize); } return $res; } getopts ('bc:hIns:pT:v', \%opts); if ( $opts{'h'} ) { printf "Usage: $0 [-bhIpvn] [-c cols] [-s size] [host ...]\n"; printf "\t-b\t\tBlack and white screen (default to autodetect)\n"; printf "\t-c columns\tHow many columns across (default is 1 host set)\n"; printf "\t-h\t\tThis help screen\n"; printf "\t-I\t\tInvert the color pallet\n"; printf "\t-n\t\tDo not execute the command (useful with -v)\n"; printf "\t-p\t\tPortrait mode (default is landscape)\n"; printf "\t-s size\t\tSize of each square (default is X / 16)\n"; printf "\t-T Title\tWindow Title (default is \"Remote status of ...\")\n"; printf "\t-v\t\tVerbose, print out the command line\n"; printf "\thost\t\tIf not specified, then use localhost\n"; exit (0); } # Fetch the screen resolution to use to determine the individual graph size my $resolution=`getresolution`; chomp $resolution; my $size = get_size($resolution); my $color=`xwininfo -root 2>&1 | egrep -c 'Visual Class: [^ ]*Color'`; $opts{'c'} = 1 if ( ! defined $opts{'c'} || $opts{'c'} <= 0 ); $color = 0 if ( $opts{'b'} ); $size = $opts{'s'} if ( defined $opts{'s'} ); if ( $#ARGV == -1 ) { @hosts = ( 'localhost' ); } else { @hosts = @ARGV; } my $cmd='xsysstats -background ' . (defined $opts{'I'}? 'white': 'black'); if ( ! $color ) { $cmd .= ' -baseline ' . (defined $opts{'I'}? 'black': 'white'); } else { $cmd .= ' -baseline blue' } ### if ( $opts{'T'} ) { $cmd .= ' -title "' . $opts{'T'} . '"'; } else { if (scalar @hosts > 1) { my $last = pop @hosts; $cmd .= ' -title "Remote status of ' . join (', ', @hosts) . ' and ' . $last . '"'; push @hosts, $last; } else { $cmd .= ' -title "Remote status of ' . $hosts[0] . '"'; } } # Calculate the layout of the graphs. $cmd .= ' -split ' . &cal_coord (scalar keys %graphs, scalar @hosts); $cmd .= ' -geometry ' . &cal_coord (scalar keys %graphs, scalar @hosts, $size); # Now add the default colors map { $cmd .= ' -defcolor ' . $_ . ' ' . ($color > 0 ? ($opts{'I'} ? $graphs{$_}->{'DarkColor'}: $graphs{$_}->{'Color'}): (defined $opts{'I'}? 'black': 'white')); } sort keys %graphs; if ( ! $opts{'p'} ) { # Now add all the hosts to our command foreach my $host (@hosts) { map { $cmd .= ' -type ' . $_ . '@' . $host; } sort keys %graphs; } } else { # Now add all the hosts to our command map { foreach my $host (@hosts) { $cmd .= ' -type ' . $_ . '@' . $host; } } sort keys %graphs; } # Display the actual xsysstats command, if the user requested it printf STDERR "$cmd\n" if ( $opts{'v'} ); # Execute the command if we were told to. exec $cmd if ( ! $opts{'n'} );