advanced process counter (perl)

Templates, scripts for templates, scripts and requests for templates.

Moderators: Developers, Moderators

Post Reply
moojoo

advanced process counter (perl)

Post by moojoo »

Hi there,

first i have to say that cacti and even rrdtool are very great tools. I'm using them since monday. So i want to share a script i wrote for counting specific processes with you:

Code: Select all

#!/usr/bin/perl -w
#
# advanced process counting script for use with cacti - by Achim Schrepfer <a.schrepfer@oncom.de>
# 10.04.2003 - Version 0.1
#
# counts process numbers for any given command; type "ps -eo cmd|awk '{print $1}'|sort|uniq" to see the command-list on your machine
#
# usage:
# proc_advanced.pl command_name1[,command_name2,...]
#
# output:
# num_procs_for_cmd1 num_procs_for_cmd2 ...

use strict;

# go away without arguments
if (!@ARGV) { exit 1; }

my %command_counter;

# initialize counters for each command
foreach (@ARGV) {
        $command_counter{ $_ } = 0;
}

# walk through the process list and count processes if exist
foreach (split(/\n/,`ps -eo cmd|awk '{print \$1}'`)) {
        $command_counter{ $_ } ++ if (exists($command_counter{ $_ }));
}

my @values;

foreach (@ARGV) {
        push @values, $command_counter{ $_ };
}

# print out process counters in same order as the arguments seperated by spaces
print join(' ',@values);
The main advantage of the script is to count more processes (like apache, mysql and cronolog) in one step to save resources. You must only type the command of the process (like /usr/sbin/apache for the apache webserver) and the script prints out the number of processes started with this command.

To see what commands are used for the specifig processes type in the following command: ps -eo cmd|awk '{print $1}'|sort|uniq

have fun :)
moojoo
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests