{"id":50,"date":"2009-08-13T13:00:00","date_gmt":"2009-08-13T18:00:00","guid":{"rendered":"http:\/\/blogs.wp.stage.cpanel.net\/2009\/08\/writing_a_multi-server_load_average_monitoring_script_in_under_10_lines_of_php\/"},"modified":"2009-08-13T13:00:00","modified_gmt":"2009-08-13T18:00:00","slug":"writing_a_multi-server_load_average_monitoring_script_in_under_10_lines_of_php","status":"publish","type":"post","link":"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_a_multi-server_load_average_monitoring_script_in_under_10_lines_of_php\/","title":{"rendered":"Writing a Multi-Server Load Average Monitoring Script in Under 10 Lines of PHP"},"content":{"rendered":"

Mostly the XML API is used for account management; however, there are other features in it that simplify system administration. The most obvious of these functions is the loadavg<\/em> call.<\/p>\n

Last year, I wrote a class for working with the XML API from PHP<\/a>. This class returns SimpleXML objects<\/a> for each XML API call made. This makes development of remote cPanel interactions extremely simple.<\/p>\n

I’m going to go over how to build a quick-and-dirty multi-server load average monitoring system in PHP using this class.<\/p>\n

For this script, we’ll store access credentials in an XML file class named monitor.xml<\/em>. The reason for using XML is so that we can store this data in an easy-to-read\/modify format (even programmatically). This is far simpler than using an array of associative arrays for doing this, as access hashes tend to be large. <\/p>\n

This is the schema that I have decided upon using:
\n<monitor>
\n<server>
\n<ip>...<\/ip>
\n<accesshash>..<\/accesshash>
\n<user>..<\/user>
\n<\/server>
\n<server>
\n..
\n<\/server>
\n<\/monitor><\/code><\/p>\n

If you are not familiar with access hashes, these correspond to the hash stored in either ~\/.accesshash <\/em>or Setup Remote Access Keys<\/em> in WHM.<\/p>\n

To load this data structure inside of our script, we want to do the following:
\n
\n$conf = simplexml_load_file(\"monitor.xml\");
\n<\/code><\/p>\n

Once the configuration of this script has been set in place, we can start on the actual logic of this script, which in this case will consist (mostly) of a loop over all of the servers in the XML.
\n foreach ( $conf->server as $server ) {
\n# Logic Here
\n}<\/code><\/p>\n

With the configuration and iteration worked out, we will want to include and instantiate the XML API class. This class only takes one parameter in its constructor: the host of the server it’s managing.
\n$conf = simplexml_load_file(\"monitor.xml\");
\ninclude(\"xmlapi.php.inc\");
\nforeach ( $conf->server as $server ) {
\n$xmlapi = new xmlapi( $server->ip );
\n}
\n<\/code><\/p>\n

Next, we will want to set up how to authenticate to the XML API.<\/p>\n

Generally speaking, WHM Auth should always be used for any type of automation for numerous reasons, namely because it does not have the same security restrictions as Basic Auth or Cookie Auth. The only reason this shouldn’t be done is client-side applications where you may not necessarily know the username and password.<\/p>\n

To do this with the XML API PHP class, you should use the hash_auth<\/em> function, which will set the appropriate headers.
\n $conf = simplexml_load_file(\"monitor.xml\");
\ninclude(\"xmlapi.php.inc\");
\nforeach ( $conf->server as $server ) {
\n$xmlapi = new xmlapi( $server->ip );
\n$xmlapi->hash_auth( $server->user, $server->accesshash);
\n}
\n<\/code><\/p>\n

Once this has been set up, we are ready to run whatever commands we need to run. Using this class, we can call loadavg<\/em> by just calling the loadavg<\/em> method within the XML API class.
\n $conf = simplexml_load_file(\"monitor.xml\");
\ninclude(\"xmlapi.php.inc\");
\nforeach ( $conf->server as $server ) {
\n$xmlapi = new xmlapi( $server->ip );
\n$xmlapi->hash_auth( $server->user, $server->accesshash);
\n$loadavg = $xmlapi->loadavg;
\n}
\n<\/code><\/p>\n

At this point, $loadavg<\/em> will contain the loadavg information, similar to what’s in \/proc\/cpuinfo<\/em>, but in a SimpleXML format. All we have to do now is display this data:
\n $conf = simplexml_load_file(\"monitor.xml\");
\ninclude(\"xmlapi.php.inc\");
\nforeach ( $conf->server as $server ) {
\n$xmlapi = new xmlapi( $server->ip );
\n$xmlapi->hash_auth( $server->user, $server->accesshash);
\n$loadavg = $xmlapi->loadavg;
\nprint \"<br>\" . $server->ip . \": \" . $loadavg->one . \", \" . $loadavg->five . \", \" . $loadavg->fifteen;
\n}
\n<\/code><\/p>\n

We are ready to run this script. Once executed, you should see something like the following for each server in monitor.xml<\/em>:
\n 127.0.0.1: 0.00, 0.00, 0.00<\/code><\/p>\n

Now that this has been written, there is a HUGE<\/strong> security issue within this script that needs to be addressed. The following line is our offending code:
\n $conf = simplexml_load_file(\"monitor.xml\");<\/code><\/p>\n

This is an issue, as it is loading monitor.xml<\/em> from within a document root (for example, $USERHOME\/public_html\/monitor.xml<\/em>). This means that anyone could download this file and then authenticate to your server’s WHM account. Instead, this file will need to be stored in a secure location outside of the document root, such as ~\/monitor.xml<\/em>.<\/p>\n

The other concern with this is that this file should always<\/strong> have permissions of 400, never readable by other users. This script should only be executed as suPHP or off of a shared hosting system, so that it cannot be read by other users on the system.<\/p>\n","protected":false},"excerpt":{"rendered":"

Mostly the XML API is used for account management; however, there are other features in it that simplify system administration. The most obvious of these functions is the loadavg call. Last year, I wrote a class for working with the XML API from PHP. This class returns SimpleXML objects for each XML API call made. […]<\/p>\n","protected":false},"author":77,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[49],"tags":[297,301,305,201,309],"class_list":["post-50","post","type-post","status-publish","format-standard","hentry","category-products","tag-php","tag-remote","tag-simple","tag-whm","tag-xml-api"],"acf":[],"yoast_head":"\nWriting a Multi-Server Load Average Monitoring Script in Under 10 Lines of PHP | cPanel<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=_-732.html \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Writing a Multi-Server Load Average Monitoring Script in Under 10 Lines of PHP | cPanel\" \/>\n<meta property=\"og:description\" content=\"Mostly the XML API is used for account management; however, there are other features in it that simplify system administration. The most obvious of these functions is the loadavg call. Last year, I wrote a class for working with the XML API from PHP. This class returns SimpleXML objects for each XML API call made. […]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_a_multi-server_load_average_monitoring_script_in_under_10_lines_of_php\/\" \/>\n<meta property=\"og:site_name\" content=\"cPanel\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cpanel\/\" \/>\n<meta property=\"article:published_time\" content=\"2009-08-13T18:00:00+00:00\" \/>\n<meta name=\"author\" content=\"cPanel Community\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@cPanel\" \/>\n<meta name=\"twitter:site\" content=\"@cPanel\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"cPanel Community\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_a_multi-server_load_average_monitoring_script_in_under_10_lines_of_php\/\",\"url\":\"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_a_multi-server_load_average_monitoring_script_in_under_10_lines_of_php\/\",\"name\":\"Writing a Multi-Server Load Average Monitoring Script in Under 10 Lines of PHP | cPanel\",\"isPartOf\":{\"@id\":\"https:\/\/devel.www.cpanel.net\/#website\"},\"datePublished\":\"2009-08-13T18:00:00+00:00\",\"dateModified\":\"2009-08-13T18:00:00+00:00\",\"author\":{\"@id\":\"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8\"},\"breadcrumb\":{\"@id\":\"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_a_multi-server_load_average_monitoring_script_in_under_10_lines_of_php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_a_multi-server_load_average_monitoring_script_in_under_10_lines_of_php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_a_multi-server_load_average_monitoring_script_in_under_10_lines_of_php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/devel.www.cpanel.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Writing a Multi-Server Load Average Monitoring Script in Under 10 Lines of PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/devel.www.cpanel.net\/#website\",\"url\":\"https:\/\/devel.www.cpanel.net\/\",\"name\":\"cPanel\",\"description\":\"Hosting Platform of Choices\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/devel.www.cpanel.net\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8\",\"name\":\"cPanel Community\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g\",\"caption\":\"cPanel Community\"},\"description\":\"The web hosting industry's most reliable management solution since 1997. With our first-class support and rich feature set, it's easy to see why our customers and partners make cPanel & WHM their hosting platform of choice. For more information, visit cPanel.net.\",\"sameAs\":[\"https:\/\/cpanel.net\"],\"url\":\"https:\/\/devel.www.cpanel.net\/blog\/author\/cpadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Writing a Multi-Server Load Average Monitoring Script in Under 10 Lines of PHP | cPanel","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_a_multi-server_load_average_monitoring_script_in_under_10_lines_of_php\/","og_locale":"en_US","og_type":"article","og_title":"Writing a Multi-Server Load Average Monitoring Script in Under 10 Lines of PHP | cPanel","og_description":"Mostly the XML API is used for account management; however, there are other features in it that simplify system administration. The most obvious of these functions is the loadavg call. Last year, I wrote a class for working with the XML API from PHP. This class returns SimpleXML objects for each XML API call made. […]","og_url":"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_a_multi-server_load_average_monitoring_script_in_under_10_lines_of_php\/","og_site_name":"cPanel","article_publisher":"https:\/\/www.facebook.com\/cpanel\/","article_published_time":"2009-08-13T18:00:00+00:00","author":"cPanel Community","twitter_card":"summary_large_image","twitter_creator":"@cPanel","twitter_site":"@cPanel","twitter_misc":{"Written by":"cPanel Community","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_a_multi-server_load_average_monitoring_script_in_under_10_lines_of_php\/","url":"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_a_multi-server_load_average_monitoring_script_in_under_10_lines_of_php\/","name":"Writing a Multi-Server Load Average Monitoring Script in Under 10 Lines of PHP | cPanel","isPartOf":{"@id":"https:\/\/devel.www.cpanel.net\/#website"},"datePublished":"2009-08-13T18:00:00+00:00","dateModified":"2009-08-13T18:00:00+00:00","author":{"@id":"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8"},"breadcrumb":{"@id":"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_a_multi-server_load_average_monitoring_script_in_under_10_lines_of_php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/devel.www.cpanel.net\/blog\/products\/writing_a_multi-server_load_average_monitoring_script_in_under_10_lines_of_php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_a_multi-server_load_average_monitoring_script_in_under_10_lines_of_php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/devel.www.cpanel.net\/"},{"@type":"ListItem","position":2,"name":"Writing a Multi-Server Load Average Monitoring Script in Under 10 Lines of PHP"}]},{"@type":"WebSite","@id":"https:\/\/devel.www.cpanel.net\/#website","url":"https:\/\/devel.www.cpanel.net\/","name":"cPanel","description":"Hosting Platform of Choices","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/devel.www.cpanel.net\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8","name":"cPanel Community","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g","caption":"cPanel Community"},"description":"The web hosting industry's most reliable management solution since 1997. With our first-class support and rich feature set, it's easy to see why our customers and partners make cPanel & WHM their hosting platform of choice. For more information, visit cPanel.net.","sameAs":["https:\/\/cpanel.net"],"url":"https:\/\/devel.www.cpanel.net\/blog\/author\/cpadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/posts\/50"}],"collection":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/users\/77"}],"replies":[{"embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/comments?post=50"}],"version-history":[{"count":0,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/posts\/50\/revisions"}],"wp:attachment":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/media?parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/categories?post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/tags?post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}