CaevatI'm no full time coder, and their are contribs that do do monitoring etc etc... However this could be of use / usefull I recently had the needed to monitor the total http processors and their size on a site that was getting clobbered by bots / spiders. Apache was been swapped with simultaneous connections that would eventually use all available ram and hang the server ( server had 12GB Ram , MaxClients setting was at default : 150 ).
I used the following code to see the current http memory usage and the number of http processors running ( apache_memory_usage_show_to_database.sh ):
#/usr/bin
ps -ylC httpd | awk '{x += $8;y += 1} END {print "Apache Memory Usage (MB): "x/1024; print "Average Proccess Size (MB): "x/((y-1)*1024)}' && echo -n 'current Apache Processes : ' && ps -C httpd --no-headers | wc -l
which shows :
Apache Memory Usage (MB): 1737.91
Average Proccess Size (MB): 75.5611
current Apache Processes : 23
I then modified this code to output the info to a database : ( created the db first ) this runs every 2minutes :
I've run it for now like follows :
nohup ./apache_memory_usage_show_to_database.sh &
Yes could be better....
#/usr/bin
while true
do
LOG=outlog.txt
OUTPUT=$(ps -ylC httpd | awk '{x += $8;y += 1} END {print "Apache Memory Usage (MB): "x/1024; print ":Average Proccess Size (MB): "x/((y-1)*1024)}' && echo -n ':Apache Processes: ' && ps -C httpd --no-headers | wc -l )
#DAY=$(date +"%F %H:%M:%S")
DAY=$(date +"%F"+"%T")
now=$(date +'%Y-%m-%d %H:%M:%S')
##echo $OUTPUT \'$DAY\'
echo $OUTPUT \'$DAY\' | awk '{print "INSERT INTO performance (mem_used, process_size, processors_used,period) VALUES ( "$5", "$10", "$13", "$14" );"}' | mysql --user=username --password=password database_name
sleep 120
done
This writes the info to the database : sample
mysql> select * from performance limit 10;
+--------+----------+--------------+-----------------+---------------------+
| number | mem_used | process_size | processors_used | period |
+--------+----------+--------------+-----------------+---------------------+
| 1 | 1091 | 47 | 23 | 2015-08-18 17:27:18 |
| 2 | 1129 | 36 | 31 | 2015-08-18 17:33:26 |
| 3 | 1221 | 45 | 27 | 2015-08-18 17:34:26 |
| 4 | 1511 | 25 | 66 | 2015-08-18 17:36:26 |
| 5 | 1010 | 39 | 26 | 2015-08-18 17:38:26 |
| 6 | 1776 | 21 | 85 | 2015-08-18 17:40:26 |
| 7 | 1204 | 39 | 31 | 2015-08-18 17:42:26 |
| 8 | 941 | 36 | 26 | 2015-08-18 17:44:26 |
| 9 | 1001 | 26 | 39 | 2015-08-18 17:46:26 |
| 10 | 926 | 40 | 23 | 2015-08-18 17:48:26 |
+--------+----------+--------------+-----------------+---------------------+
OK , so now i have the data been monitored, I now need a way to present / view this in an ibay :
Google Charts can help :
Create an iBay to holder the scripts :
Now create the scripts ( I am only monitoring the LAST 48 Hrs of data ):
Script to monitor http memory usage (mysqli_apache_memory_used.php ) :
<?php
// Hostname: 127.0.0.1, username: your_user, password: your_pass, db: your_db
$mysqli = new mysqli('localhost', 'username', 'password', 'db_name');
// Oh no! A connect_errno exists so the connection attempt failed!
if ($mysqli->connect_errno) {
// The connection failed. What do you want to do?
// You could contact yourself (email?), log the error, show a nice page, etc.
// You do not want to reveal sensitive information
// Let's try this:
echo "Sorry, this website is experiencing problems.";
// Something you should not do on a public site, but this example will show you
// anyways, is print out MySQL error related information -- you might log this
echo "Error: Failed to make a MySQL connection, here is why: \n";
echo "Errno: " . $mysqli->connect_errno . "\n";
echo "Error: " . $mysqli->connect_error . "\n";
// You might want to show them something nice, but we will simply exit
exit;
}
// Perform an SQL query
//$query = "SELECT number, period, process_size, processors_used FROM performance LIMIT 50";
$query ="SELECT number, period, process_size, processors_used, mem_used FROM performance WHERE period > DATE_SUB(NOW(), INTERVAL 48 HOUR)";
if (!$result = $mysqli->query($query)) {
// Oh no! The query failed.
echo "Sorry, the website is experiencing problems.";
// Again, do not do this on a public site, but we'll show you how
// to get the error information
echo "Error: Our query failed to execute and here is why: \n";
echo "Query: " . $sql . "\n";
echo "Errno: " . $mysqli->errno . "\n";
echo "Error: " . $mysqli->error . "\n";
exit;
}
// Phew, we made it. We know our MySQL connection and query
// succeeded, but do we have a result?
if ($result->num_rows === 0) {
// Oh, no rows! Sometimes that's expected and okay, sometimes
// it is not. You decide. In this case, maybe actor_id was too
// large?
echo "We could not find a match , sorry about that. Please try again.";
exit;
}
$qresult = $mysqli->query($query);
$results = array();
while($res = $qresult->fetch_assoc()) {
$results[] = $res;
}
$curvechart_data = array();
foreach($results as $result)
{
//$curvechart_data[] = array($result['period'],(int)$result['process_size'], (int)$result['processors_used'],(int)$result['mem_used']);
$curvechart_data[] = array($result['period'],(int)$result['mem_used']);
}
$curvechart_data = json_encode($curvechart_data);
//echo $curvechart_data;
// The script will automatically free the result and close the MySQL
// connection when it exits, but let's just do it anyways
//$result->free();
//$mysqli->close();
$html =<<<XYZ
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1.1', {packages: ['line']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string','Period');
//data.addColumn('number', 'No. Processors');
//data.addColumn('number', 'Process size');
data.addColumn('number', 'Memory Used');
data.addRows({$curvechart_data});
var options = {
chart: {
title: 'Apache HTTP Memory used ',
subtitle: 'in last 48 Hrs '
},colors:['red']
width: 1400,
height: 700
};
var chart = new google.charts.Line(document.getElementById('linechart_material'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="linechart_material"></div>
</body>
</html>
XYZ;
echo $html;
echo date('l jS \of F Y h:i:s A');
?>
Now goto your ibay and you can view the chart ( Sample chart attached ):
http://server-name/ibay/mysqli_apache_memory_used.php
Hopefully useful to all