Change Detect and Alert

Started on the old forum in May 2011 · view the original in the archive · old links redirect here

#alert #change #detect #sms

1 reply · 559 views · started 15 years ago · 3 min read

BioBeo Developer · 67 posts #1

Purpose: detect filesystem changes on a (web)server and send list of changed or added files via email. Can send a kind of alert as SMS if your mobile network operator supports email to sms gateway. Tip: see Wikipedia for "List of SMS gateways".

Usage:

Change script settings (in file) according to your needs Upload to your (web)server Setup cron job to run it periodically;

Notes: script checks file size only, will possibly add date and contents comparison later if needed; invoking via PHP is preferred; you may need to specify absolute path to php or curl Requirements: Change Detect and Alert is a script which is supposed to be run on a server with PHP and SQLite3 installed.

// Specify your email here. Use comma as delimiter if you want to sent to multiple emails
define('EMAIL', 'youremail@example.com');

// Specify path from the root folder where script will store its database, NO ENDING SLASH
define('DBPATH', '/usr/home/my-site/www');

// Filename of database (no path)
define('DBNAME', 'comments.idx');

// folder being tracked, NO ENDING SLASH
define('CHECK_PATH', '/usr/home/my-site/www/uploads');

// true - check all subfolders of CHECK_PATH
// false - do not check subfolders
define('CHECK_RECURSIVE', true);

###############################################################
## YOU DO NOT HAVE TO CHANGE ANYTHING BELOW UNLESS YOU KNOW WHAT YOU ARE DOING
###############################################################

define('DBFILE', DBPATH . '/' .DBNAME);
define('CHG_UNCHANGED', 'UNCHANGED');
define('CHG_ADDED', 'ADDED');
define('CHG_CHANGED', 'CHANGED');

function createDb() {
  $db = new SQLiteDatabase(DBFILE);
  $db->queryExec("CREATE TABLE tbl_files (f_name TEXT, f_size INTEGER, PRIMARY KEY (f_name))");
}

function sendEmail($changesBody, $time) {

  if (empty($changesBody)) return;

  // set email headers
  $headers  = "MIME-Version: 1.0\r\n";
  $headers .= "Content-type: text/plain; charset=\"us-ascii\"\r\n";
  $headers .= "From: " . EMAIL . "\r\n";
  $headers .= "Reply-To: " . EMAIL . "\r\n";
  $headers .= 'X-Mailer: Change Detect and Alert';

  $message = $changesBody . "
 Job took from start to finish: " . $time;

  if (EMAIL != '') {
    // send email
    @mail(EMAIL,              // TO email
      'Server Change Alert',  // subject
      $message,               // email text
      $headers);              // headers
  }
}

function isChanged($filename) {
  if (strpos($filename, DBNAME) != FALSE) return '';

  $isChanged = CHG_UNCHANGED;
  $db = new SQLiteDatabase(DBFILE);
  $q = $db->query("SELECT f_size FROM tbl_files WHERE f_name = '$filename' ");

  $filesize_old = $q->fetchSingle();
  $filesize_new = filesize($filename);

  // not in db?
  if ($filesize_old === FALSE) {
    $isChanged = CHG_ADDED;
  }
  else if (filesize($filename) != $filesize_old) {
    $isChanged = CHG_CHANGED;
  }
  // update database
  if ($isChanged != CHG_UNCHANGED) {
    if ($isChanged == CHG_ADDED) {
      $db->queryExec("INSERT INTO tbl_files VALUES ('$filename', " . filesize($filename) . " )");
    }
    else if ($isChanged == CHG_CHANGED) {
      $db->queryExec("UPDATE tbl_files SET f_size = " . filesize($filename) . " WHERE f_name = '$filename'");
    }
  }

  if ($isChanged != CHG_UNCHANGED) {
    if (!empty($_GET['web'])) echo "$filename -- $isChanged (was:" . $filesize_old . '/is:' . $filesize_new . ")<br>";
    return "$filename -- $isChanged (was:" . $filesize_old . '/is:' . $filesize_new . ")\n";
  }
  else
    return "";
}

$changesBody = "";

function loopDir($dirname) {
  global $changesBody;

  $dir = opendir($dirname);

  while ($file = readdir($dir)) {
    if ($file != '.' && $file != '..') {
      if (is_dir($dirname.'/'.$file)) {
        if (CHECK_RECURSIVE) {
          loopDir($dirname.'/'.$file);
        }
      } else {
        $changesBody = $changesBody . isChanged($dirname.'/'.$file);
      }
    }
  }
}

if (!file_exists(DBFILE)) {
  createDb();
  sendEmail("Database file not found! Creating.", 0);
}

$stime = microtime(true);
loopDir(CHECK_PATH);
sendEmail($changesBody, microtime(true) - $stime);

echo "\nDONE";
?>

I use it to detect ebook uploads on my ftp server. The only change i made: invoke external program in addition to chnages email. Really helpful...

No one could help you If you don't help yourself. My facebook

BioBeo Developer · 67 posts #2

again this helps...

No one could help you If you don't help yourself. My facebook

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close