|
Packit |
667938 |
#!/usr/bin/perl
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# ============================================================================
|
|
Packit |
667938 |
# CPU Usage script for mrtg
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# File: cpuinfo.pl
|
|
Packit |
667938 |
# Author: Matthew Schumacher | schu@schu.net
|
|
Packit |
667938 |
# Version: 1.3
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# Date: 8/17/2000
|
|
Packit |
667938 |
# Purpose: This script reports CPU usage for user
|
|
Packit |
667938 |
# and system to mrtg, along with uptime
|
|
Packit |
667938 |
# and the machine's hostname.
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# Usage: ./cpuinfo.pl [machine] [os]
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# For now [os] can only be "sun" or "linux"
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# Info: Designed on RedHat linux 6.2 with perl
|
|
Packit |
667938 |
# version 5.005_03. The script itself has
|
|
Packit |
667938 |
# only been tested on Linux, however, it
|
|
Packit |
667938 |
# has been tested to connect to, and graph
|
|
Packit |
667938 |
# CPU usage on sun and linux.
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# This script requires both sar and rsh to
|
|
Packit |
667938 |
# be installed and working. Because linux
|
|
Packit |
667938 |
# does not come with sar (mine didn't) it
|
|
Packit |
667938 |
# may be necessary to download and install
|
|
Packit |
667938 |
# it. Get sar here:
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# ftp://metalab.unc.edu/pub/Linux/system/status/sysstat-3.2.4.tar.gz
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# How it works:
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# The script uses rsh (or ssh) to run sar on the the
|
|
Packit |
667938 |
# remote machine. Sar samples the cpu time
|
|
Packit |
667938 |
# for both user and system once per second
|
|
Packit |
667938 |
# for 10 seconds. It then reports an average
|
|
Packit |
667938 |
# to the script, which parses out the information
|
|
Packit |
667938 |
# and formats it in a way mrtg can understand.
|
|
Packit |
667938 |
# The script also runs uptime to get the machine's
|
|
Packit |
667938 |
# uptime and passes it to mrtg.
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# [History]
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# 1/4/2000 - Added support for different rsh programs.
|
|
Packit |
667938 |
# I also made the default rsh program ssh for security
|
|
Packit |
667938 |
# reasons.
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# 3/9/2000 - Removed the default os because it seemed
|
|
Packit |
667938 |
# redundant. Added code to support localhost as a machine
|
|
Packit |
667938 |
# name.
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# 8/17/2000 - Updated sar regex for sar command. Sorry
|
|
Packit |
667938 |
# for not keeping up with the email, I had some email
|
|
Packit |
667938 |
# issues then finnaly I got my own domain and changed it
|
|
Packit |
667938 |
# to schu@schu.net.
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# ============================================================================
|
|
Packit |
667938 |
# Sample cfg:
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# WorkDir: /home/httpd/html/mrtg
|
|
Packit |
667938 |
# Target[machine]: `/home/mrtg/run/cpuinfo.pl localhost linux`
|
|
Packit |
667938 |
# MaxBytes[machine]: 100
|
|
Packit |
667938 |
# Options[machine]: gauge, nopercent
|
|
Packit |
667938 |
# Unscaled[machine]: dwym
|
|
Packit |
667938 |
# YLegend[machine]: % of CPU used
|
|
Packit |
667938 |
# ShortLegend[machine]: %
|
|
Packit |
667938 |
# LegendO[machine]: CPU System:
|
|
Packit |
667938 |
# LegendI[machine]: CPU User:
|
|
Packit |
667938 |
# Title[machine]: Machine name
|
|
Packit |
667938 |
# PageTop[machine]: CPU usage for machine (schu's workstation)
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# System:Machine
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
#
|
|
Packit |
667938 |
# ============================================================================
|
|
Packit |
667938 |
# setup local vars
|
|
Packit |
667938 |
my($machine, $os);
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# ============================================================================
|
|
Packit |
667938 |
# == Enter your rsh program here here ==
|
|
Packit |
667938 |
|
|
Packit |
667938 |
$rsh = "/usr/local/bin/ssh -x"; # Enter your rsh command here
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# == You shouldn't need to edit anything below this line ==
|
|
Packit |
667938 |
#========================================================
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# This checks for options passed cpuinfo.pl from the cmd line
|
|
Packit |
667938 |
if (scalar(@ARGV) < 2)
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
print("USAGE: cpuinfo.pl {machine} {os}\n");
|
|
Packit |
667938 |
exit(-1);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
if ($ARGV[0] ne '' && $ARGV[0] ne '#')
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
$machine = $ARGV[0];
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
if ($ARGV[1] ne '' && $ARGV[1] ne '#')
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
$os = $ARGV[1];
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# Validate the os
|
|
Packit |
667938 |
SWITCH:
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
if ($os =~ /^sun$/){last SWITCH;}
|
|
Packit |
667938 |
if ($os =~ /^linux$/){last SWITCH;}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# DEFAULT: Die if we can't figure out what the os is
|
|
Packit |
667938 |
die "Can't figure out which OS the machine is.\n";
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# Execute the appropriate subroutine based on the os
|
|
Packit |
667938 |
&$os;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
exit(0);
|
|
Packit |
667938 |
|
|
Packit |
667938 |
#=======================================================
|
|
Packit |
667938 |
# Subroutines: names of subroutines are supported OSs.
|
|
Packit |
667938 |
#========================================================
|
|
Packit |
667938 |
sub sun
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# Run commands
|
|
Packit |
667938 |
if ($machine =~ 'localhost')
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
$getcpu = `sar -u 1 10 | grep Average`;
|
|
Packit |
667938 |
$getuptime = `uptime`;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
else
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
$getcpu = `$rsh $machine "sar -u 1 10" | grep Average`;
|
|
Packit |
667938 |
$getuptime = `$rsh $machine "uptime"`;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# Parse though getcpu and get data
|
|
Packit |
667938 |
$getcpu =~ /^Average\s+(\d+)\s+(\d+)\s+/;
|
|
Packit |
667938 |
$outputusr = $1;
|
|
Packit |
667938 |
$outputsys = $2;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# Print getcpu data for mrtg
|
|
Packit |
667938 |
print $outputusr."\n";
|
|
Packit |
667938 |
print $outputsys."\n";
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# Parse though getuptime and get data
|
|
Packit |
667938 |
$getuptime =~ /^\s+\d{1,2}:\d{2}..\s+up\s+(\d+)\s+(......),/;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# Print getuptime data for mrtg
|
|
Packit |
667938 |
print $1." ".$2."\n";
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# Print machine name for mrtg
|
|
Packit |
667938 |
print $machine."\n";
|
|
Packit |
667938 |
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub linux
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
# Run commands
|
|
Packit |
667938 |
if ($machine =~ 'localhost')
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
$getcpu = `/usr/local/bin/sar -u 1 10 | grep Average`;
|
|
Packit |
667938 |
$getuptime = `/usr/bin/uptime`;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
else
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
$getcpu = `$rsh $machine "/usr/local/bin/sar -u 1 10 | grep Average"`;
|
|
Packit |
667938 |
$getuptime = `$rsh $machine "/usr/bin/uptime"`;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# Parse though getcpu and get data
|
|
Packit |
667938 |
$getcpu =~ /^Average:\s+all\s+(\d+)\.\d+\s+\d+\.\d+\s+(\d+)\.\d+\s+\d+\.\d+/;
|
|
Packit |
667938 |
$getcpuusr = $1;
|
|
Packit |
667938 |
$getcpusys = $2;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# Print getcpu data for mrtg
|
|
Packit |
667938 |
print $getcpuusr."\n";
|
|
Packit |
667938 |
print $getcpusys."\n";
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# Parse though getuptime and get data
|
|
Packit |
667938 |
$getuptime =~ /^\s+\d{1,2}:\d{2}..\s+up\s+(\d+)\s+(\w+),/;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# Print getuptime data for mrtg
|
|
Packit |
667938 |
print $1." ".$2."\n";
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# Print machine name for mrtg
|
|
Packit |
667938 |
print $machine."\n";
|
|
Packit |
667938 |
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
exit(0);
|