http service check (php & db)

最後更新: 2016-03-15



此 script 既好處係 check 到 DB 有沒有事.



DB: schema

CREATE DATABASE `systemcheck`;

USE `systemcheck`;

CREATE TABLE `checktime` (
  `id` int(10) unsigned NOT NULL,
  `lastcheck` varchar(255) DEFAULT NULL

INSERT INTO `checktime` (`id`, `lastcheck`) VALUES
(0, '0000000000');

GRANT SELECT, INSERT, UPDATE, DELETE, LOCK TABLES ON `systemcheck`.* TO 'systemcheck'@'localhost';


Code: systemcheck.php

// file: systemcheck.php
$username = "";
$password = "";
$host = "localhost";
$db = "systemcheck";
$tb = "checktime";

function alarm($msg) {
  header('HTTP/1.1 500 Internal Server Error');

//connection to the database
$dbhandle = mysql_connect($host, $username, $password)
 or alarm("Unable to connect to MySQL");

//select a database to work with
$selected = mysql_select_db($db,$dbhandle)
 or alarm("Could not select DB");

//execute the SQL query and return records
$result = mysql_query("SELECT lastcheck FROM $tb;")
 or alarm("Could not select Table");

// write record to db
$now = time();
$result = mysql_query("UPDATE $tb set lastcheck = $now;")
 or alarm("Could not update Table");

//close the connection

echo "<html><h1>Server is working</h1></html>"





# 假設 Server 既 URL 係 http://myserver/systemcheck.php

./check_http -H myserver -u /systemcheck.php