#!/usr/bin/perl -Tw # cpu-solaris.pl #################################################### # # MRTG Performance Enhancements v1.0.2 # # This script grabs Solaris Performance Monitoring (PM) data for MRTG # on the CPU utilization of the system. # # Usage: cpu-solaris.pl [hostname] # Example: ./cpu-solaris.pl myhost # # By: # Mark Miller, Crave Technology markm@cravetechnology.com # Bill Lynch, Crave Technology billl@cravetechnology.com ##################################################################### # ========================= =pod ---++ sub formatTime ($epochSeconds, $formatString, $outputTimeZone) ==> $value | $epochSeconds | epochSecs GMT | | $formatString | twiki time date format | | $outputTimeZone | timezone to display. (not sure this will work)(gmtime or servertime) | =cut sub formatTime { my ($epochSeconds, $formatString, $outputTimeZone) = @_; my $value = $epochSeconds; # use default TWiki format "31 Dec 1999 - 23:59" unless specified $formatString = "\$day \$month \$year - \$hour:\$min" unless( $formatString ); $outputTimeZone = $displayTimeValues unless( $outputTimeZone ); my( $sec, $min, $hour, $day, $mon, $year, $wday) = gmtime( $epochSeconds ); ( $sec, $min, $hour, $day, $mon, $year, $wday ) = localtime( $epochSeconds ) if( $outputTimeZone eq "servertime" ); #standard twiki date time formats if( $formatString =~ /rcs/i ) { # RCS format, example: "2001/12/31 23:59:59" $formatString = "\$year/\$mo/\$day \$hour:\$min:\$sec"; } elsif ( $formatString =~ /http|email/i ) { # HTTP header format, e.g. "Thu, 23 Jul 1998 07:21:56 EST" # - based on RFC 2616/1123 and HTTP::Date; also used # by TWiki::Net for Date header in emails. $formatString = "\$wday, \$day \$month \$year \$hour:\$min:\$sec \$tz"; } elsif ( $formatString =~ /iso/i ) { # ISO Format, see spec at http://www.w3.org/TR/NOTE-datetime # e.g. "2002-12-31T19:30Z" $formatString = "\$year-\$mo-\$dayT\$hour:\$min"; if( $outputTimeZone eq "gmtime" ) { $formatString = $formatString."Z"; } else { #TODO: $formatString = $formatString. # TZD = time zone designator (Z or +hh:mm or -hh:mm) } } $value = $formatString; $value =~ s/\$sec[o]?[n]?[d]?[s]?/sprintf("%.2u",$sec)/geoi; $value =~ s/\$min[u]?[t]?[e]?[s]?/sprintf("%.2u",$min)/geoi; $value =~ s/\$hou[r]?[s]?/sprintf("%.2u",$hour)/geoi; $value =~ s/\$day/sprintf("%.2u",$day)/geoi; my @weekDay = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"); $value =~ s/\$wday/$weekDay[$wday]/geoi; my @isoMonth = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ); $value =~ s/\$mon[t]?[h]?/$isoMonth[$mon]/goi; $value =~ s/\$mo/sprintf("%.2u",$mon+1)/geoi; $value =~ s/\$yea[r]?/sprintf("%.4u",$year+1900)/geoi; $value =~ s/\$ye/sprintf("%.2u",$year%100)/geoi; #TODO: how do we get the different timezone strings (and when we add usertime, then what?) my $tz_str = "GMT"; $tz_str = "Local" if ( $outputTimeZone eq "servertime" ); $value =~ s/\$tz/$tz_str/geoi; return $value; } ##### Subroutines ##### # Grab the PM data locally sub localh { $vmstat = `vmstat 1 2`; $uptime = `uptime`; } # Grab the PM data remotely sub remoteh { $vmstat = `/opt/csw/bin/ssh $host vmstat 1 2`; $uptime = `/opt/csw/bin/ssh $host uptime`; } sub Usage() { #display correct usage info print "Usage: cpu-solaris.pl hostname\n"; print " ex: ./cpu-solaris.pl myhost\n"; } sub Validate() { #validate command line args if ((scalar(@ARGV) > 1) || ($ARGV[0] eq '')) { print "ERR: Must specify host to be monitored.\n"; Usage; exit; } } sub parseuptime() { # This sub returns only the number of days of uptime # A box with less than 1 day of uptime will show 0 days of uptime $uptime=~s/,//g; @utime = split /\s/, $uptime; for ($i = 1; $i < 8; $i++) { if ($utime[$i] eq "days" || $utime[$i] eq "day(s)" || $utime[$i] eq "day") { $upout = $utime[$i-1]." day(s)"; } } if ($upout eq "") { $upout = "0 day(s)"; } } sub getNumberOfRequests { my ($logFile, $timeRegex) = @_; my $total_nunber_of_requests = 0; my $total_number_of_twikiguest_requests = 0; my $linesProcessed = 0; my %pos; if (-e 'twiki_seek.cfg' ) { open(seekFile, 'twiki_seek.cfg'); while() { if ( /^(.*): (\d*)$/ ) { $pos{$1} = $2; } } close(seekFile); } open(logFile, $logFile); if ( $pos{$logFile} ) { seek(logFile, $pos{$logFile}, 0); } my @logLines = ; $pos{$logFile} = tell(logFile); close(logFile); open(seekFile, '>twiki_seek.cfg'); foreach my $fileName (keys %pos) { print seekFile $fileName.': '.$pos{$fileName}."\n"; } close(seekFile); foreach my $line (@logLines) { $linesProcessed++; my ($before, $date, $user, $oper, $topic, $browser, $host) = split(/\|/, $line); if ( $date =~ /$timeRegex/ ) { $total_nunber_of_requests++; if ( $user =~ /TWikiGuest/ ) { $total_number_of_twikiguest_requests++; } } } return ($total_nunber_of_requests, $total_number_of_twikiguest_requests); } ##### Main Program Begins Here ###### # Get the hostnames #$localhost = `/bin/hostname`; #chop($localhost); #$host = $ARGV[0]; # Determine the short hostname #@local = split(/\./,$localhost); #$shorthost = $local[0]; #open the twiki log file for this month, and report on size? my $systemTime = time();# - (5*24*60*60); #5 days ago for testing.. my $log = "/home/sven/backups/twiki.org/data/log%DATE%.txt"; my $previousLog; my $logTime = formatTime( $systemTime, '$year$mo', 'servertime'); $log =~ s/%DATE%/$logTime/go; my $total_number_of_requests = 0; my $total_number_of_twikiguest_requests = 0; my $timeRegex = '(?:'; for (my $count = 0;$count < 5;$count++) { my $time = formatTime( $systemTime-($count*60), '$day $mon $year - $hour:$min' , 'servertime' ); $timeRegex = $timeRegex.' '.$time.' |'; $time = formatTime( $systemTime-($count*60), '$year$mo' , 'servertime' ); if ( $time ne $logTime ) { $previousLog = "/home/sven/backups/twiki.org/data/log%DATE%.txt"; $log =~ s/%DATE%/$time/go; } # my $number_of_requests = "grep \"$time\" $log | wc -l"; #this is a _very_ bad way to do it.. # my $number_of_twikiguest_requests = "grep \"$time\" $log | grep -i TWikiGuest | wc -l"; #this is a _very_ bad way to do it.. # print "$number_of_requests \n"; # $total_number_of_requests += `$number_of_requests`; # $total_number_of_twikiguest_requests += `$number_of_twikiguest_requests`; } $timeRegex = $timeRegex.'^$)'; ($total_number_of_requests, $total_number_of_twikiguest_requests) = getNumberOfRequests($log, $timeRegex); if ( $previousLog ) { my ($prev_total_number_of_requests, $prev_total_number_of_twikiguest_requests) = getNumberOfRequests($previousLog, $timeRegex); $total_number_of_requests += $prev_total_number_of_requests; $total_number_of_twikiguest_requests += $prev_total_number_of_twikiguest_requests; } #number of TWikiGuest requests print $total_number_of_twikiguest_requests."\n"; #number of registered user requests print ($total_number_of_requests-$total_number_of_twikiguest_requests); print "\n"; # Parse uptime data #parseuptime(); # Print uptime data in MRTG format #print $upout."\n";