Koozali.org: home of the SME Server

Google Charts in ibay

Offline warren

  • *
  • 293
  • +0/-0
Google Charts in ibay
« on: November 26, 2015, 12:48:38 PM »
Caevat
I'm no full time coder, and their are contribs that do do monitoring etc etc... However this could be of use / usefull  8-)

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 ):

Code: [Select]
#/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 :

Code: [Select]
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 :
Code: [Select]
nohup ./apache_memory_usage_show_to_database.sh &   Yes could be better....


Code: [Select]
#/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
Code: [Select]
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 ) :

Code: [Select]
<?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 ):

Code: [Select]
http://server-name/ibay/mysqli_apache_memory_used.php


Hopefully useful to all

Offline ReetP

  • *
  • 3,740
  • +5/-0
Re: Google Charts in ibay
« Reply #1 on: November 26, 2015, 06:41:11 PM »
Nice.

I'm sure someone will have a look at your code :-)

Can you add that as a Wiki page ?

Someone like Stephane may even be tempted to play, if we can get him away from his new baby girl !
...
1. Read the Manual
2. Read the Wiki
3. Don't ask for support on Unsupported versions of software
4. I have a job, wife, and kids and do this in my spare time. If you want something fixed, please help.

Bugs are easier than you think: http://wiki.contribs.org/Bugzilla_Help

If you love SME and don't want to lose it, join in: http://wiki.contribs.org/Koozali_Foundation

Offline warren

  • *
  • 293
  • +0/-0
Re: Google Charts in ibay
« Reply #2 on: November 27, 2015, 12:07:48 AM »
Wiki page Created ( almost tool longer to create the wiki page than the actual content )  :lol:
 
http://wiki.contribs.org/Google_charts

Feel free to edit / correct.

Note ! wiki seemed to eat some of the lines in the code block - 9th from bottom (  <div id="linechart_material"></div> ) :
Code: [Select]
<?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');

?>

Offline ReetP

  • *
  • 3,740
  • +5/-0
Re: Google Charts in ibay
« Reply #3 on: November 27, 2015, 01:01:17 AM »
Wiki page Created ( almost tool longer to create the wiki page than the actual content )  :lol:

Situation normal then ! Good work :-)

Quote

http://wiki.contribs.org/Google_charts

Feel free to edit / correct.

Note ! wiki seemed to eat some of the lines in the code block - 9th from bottom (  <div id="linechart_material"></div> ) :

A wiki monster will jump on it soon I am sure. HSF is the man

Many thanks for your hard work.

B Rgds
John
...
1. Read the Manual
2. Read the Wiki
3. Don't ask for support on Unsupported versions of software
4. I have a job, wife, and kids and do this in my spare time. If you want something fixed, please help.

Bugs are easier than you think: http://wiki.contribs.org/Bugzilla_Help

If you love SME and don't want to lose it, join in: http://wiki.contribs.org/Koozali_Foundation