Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need a script to email me if the load is over 10 1

Status
Not open for further replies.

farley99

MIS
Joined
Feb 12, 2003
Messages
413
Location
US
need a script to email me if the load is over 10
or if a process is using 99% of the cpu.

How can i do this?
 
Thin on background info.

I'll help prompt some responses then....

What are we talking, hair dryers, chainsaws, buses computers?

What O/S? Mac unix/linux:Redhat|debian../windozeNT|XP|2000|98|95 etc

What load? PSI/KG/NM/processor

Specific process? mysql/apache/another

How is the load info generated? command/guesswork/carrier pigeon

When would you like an email (immediately / daily / weekly etc)?



______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Sorry......

What are we talking, hair dryers, chainsaws, buses computers?
>>Computers

What O/S? Mac unix/linux:Redhat|debian../windozeNT|XP|2000|98|95 etc
>>Redhat

What load? PSI/KG/NM/processor
>>Processor (top)

Specific process? mysql/apache/another
>>all(top)

How is the load info generated? command/guesswork/carrier pigeon
>>command (top)

When would you like an email (immediately / daily / weekly etc)?
>>Every 5 minutes via a cron

Thanks
 
I wouldn't use the output of top. I'd just calculate the values from the Linux pseudofile /proc/stat. It's where top gets the numbers anyway.

Your /proc/stat should have a line which reads:

cpu 38167498 13286 11429814 1296167908

These are a set of counters that record the number of jiffies (1/100ths of a second) that are devoted to user, nice, system, and idle processes, respectively (for more info, check out "man 5 proc").

Read the part of your /proc/stat that contains the cpu jiffie allocation counters. Close the file. Wait a small amount of time (say, 5 seconds). Read the line again.

You can then calculate the difference in each counter, then divide each difference by the total of all 4 differences, then optionally multiply each dividend by 100 to get a percentage.

If the idle amount gets below 90%, you can infer that active processor load is above 10%.

Something like this:
Code:
<?php
$fh = fopen ('/proc/stat', 'r');
$beg_line = fgets ($fh);
fclose ($fh);

sleep(5);

$fh = fopen ('/proc/stat', 'r');
$end_line = fgets ($fh);
fclose ($fh);

$beg_array = preg_split ('/ +/', $beg_line);
$end_array = preg_split ('/ +/', $end_line);

$delta_array =
array(
	'user' => $end_array[1] - $beg_array[1],
	'nice' => $end_array[2] - $beg_array[2],
	'system' => $end_array[3] - $beg_array[3],
	'idle' => $end_array[4] - $beg_array[4]
);

$summa_delta = 0;
foreach ($delta_array as $element)
	$summa_delta += $element;

foreach ($delta_array as $index => $value)
	$delta_array[$index] /= $summa_delta;

print_r ($delta_array);
?>

On my system, the &quot;cpu&quot; line is the first line of /proc/stat. You may have to adjust the code to read a different line.


Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top