PHP is a good language to manage remote/local server. Below code shows how to restart daemons like httpd, mysqld, Confluence and Jira by PHP. |
In order to implement, SSH2 extension must be installed. For more information, please refer at "Run shell commands on remote server in SSH".
Below is re-usable code named as "ssh_init.php" for remote management.
<?php // Programmed by Chun Kang (kurapa@kurapa.com) on Nov 2018 $m_config->session = null; function get_line_count($r) { $i=0; $len = strlen($r); $cnt=1; while($i<$len) { if (ord($r[$i])==10) $cnt++; $i++; } if ($cnt>40) $cnt=40; return $cnt; } function ssh2_run_cmd($cmd, $include_text_area=0) { global $m_config; if ($m_config->session==NULL) { $m_config->session = ssh2_connect( $m_config->server, $m_config->port); if (!$m_config->session) { die( "{$m_config->id}@{$m_config->server}:{$m_config->port}...connection failure!!<br>\n"); } else { if ($include_text_area) echo "{$m_config->id}@{$m_config->server}:{$m_config->port}...connection ok<br>\n"; } if (ssh2_auth_password( $m_config->session, $m_config->id, $m_config->password)) { if ($include_text_area) echo "Authentication successful!<br>\n"; } else { die( "Authentication failure..."); } } if ($include_text_area) echo "<pre>{$cmd}</pre>\n"; $stream=ssh2_exec( $m_config->session, $cmd); stream_set_blocking($stream, true); $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO); $r=stream_get_contents( $stream_out); if ($include_text_area) { $rows_cnt = get_line_count( $r); return "<textarea cols=120 rows={$rows_cnt} wrap=off style='width:100%'>{$r}</textarea>\n"; } else { return $r; } } function kssh2_disconnect() { ssh2_disconnect( $m_config->session); $m_config->session=NULL; } ?> |
Below code actually performs remote process and reboot services. Actually I am checking httpd, mysqld, two confluences and one Jira instance on below code.
<?php // Programmed by Chun Kang (kurapa@kurapa.com) on Nov 2018 include_once "ssh_init.php"; $m_config->id = "<your user id>"; $m_config->password = "<user password>"; $m_config->server = "<server address>"; $m_config->port = 22; function service_status($service_name, $message, $prc) { $message = str_replace( "/", "\\/", $message); if (preg_match( "/{$message}/", $prc)) { echo "{$service_name} ok!\n"; return 1; } else { echo "{$service_name} does not work!!\n"; return 0; } } // collect process information by "ps" command $prc=ssh2_run_cmd( "ps aux --sort=-pcpu"); if (strlen($prc)>0) { echo "Process Status<br>\n"; if (!service_status("httpd", "/usr/sbin/httpd -DFOREGROUND", $prc)) { ssh2_run_cmd( "/etc/init.d/httpd restart"); echo "httpd restared!\n"; } if (!service_status("mysqld", "/usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql", $prc)) { ssh2_run_cmd( "/etc/init.d/mysqld restart"); echo "mysqld restarted!\n"; } if (!service_status("qsok.com - confluence server", "/pub/qsok.com-confluence/jre//bin/java", $prc)) { ssh2_run_cmd( "/etc/init.d/confluence restart"); echo "qsok.com restarted!\n"; } if (!service_status("enewtown.com - confluence server", "/pub/enewtown.com-confluence/jre//bin/java", $prc)) { ssh2_run_cmd( "/etc/init.d/confluence1 restart"); echo "enewtown.com restarted!\n"; } if (!service_status("qsok.com - jira server", "/pub/qsok.com-jira/jre//bin/java", $prc)) { ssh2_run_cmd( "/etc/init.d/jira restart"); echo "qsok.com restarted!\n"; } echo "<hr>"; } ?> |
In my case, I made a clone job to run above bot code to restart server instances automatically.