Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define ("LOAD_AVERAGE_THRESHOLD", 10.0);
- define ("APACHE_RESTART_THRESHOLD", 60 * 30);
- define ("DEFAULT_LOAD_AVERAGE_PATH", "/proc/loadavg");
- define ("APACHE_COMMAND_STOP", '/etc/init.d/apache2 stop');
- define ("APACHE_COMMAND_START", '/etc/init.d/apache2 start');
- define ("APACHE_COMMAND_RESTART", '/etc/init.d/apache2 restart');
- define ("APACHE_COMMAND_STATUS", '/etc/init.d/apache2 status');
- function get_current_load_average() {
- $load_average_file_name = DEFAULT_LOAD_AVERAGE_PATH;
- $fp = @fopen($load_average_file_name, 'r');
- if ($fp == null) {
- die("unable to open file at $load_average_file_name\n");
- }
- $load_averages = explode(" ", fread($fp, 4096));
- return floatval($load_averages[0]);
- }
- function restart_apache_server() {
- // Do some stuff in here!
- printf ("Server might be going through problems...restart the web server now!\n");
- system(APACHE_COMMAND_STOP);
- printf ("Stopped web server");
- sleep(10);
- system(APACHE_COMMAND_START);
- printf ("Started web server");
- }
- $allowed_restart_time = time();
- while (1) {
- $current_load_average = get_current_load_average();
- printf("Current load - %f\n", $current_load_average);
- if ($current_load_average < LOAD_AVERAGE_THRESHOLD) {
- printf ("Server is doing pretty well!\n");
- }
- else if ($current_load_average >= LOAD_AVERAGE_THRESHOLD && time() > $allowed_restart_time) {
- restart_apache_server();
- $allowed_restart_time = time() + APACHE_RESTART_THRESHOLD;
- }
- else {
- printf ("Wait for some time to ensure server is getting back to normal.\n");
- }
- sleep(30);
- }
Add Comment
Please, Sign In to add comment