#!/usr/bin/php
<?php
# do NOT run this script through a web browser
if (!isset($_SERVER["argv"][0])) {
die("<br><strong>This script is only meant to run at the command line.</strong>");
}
include_once("include/dbconnect.php");
include_once("include/functions_plugins.php");
#check if is already running.
$running_flag = array();
exec ('pgrep push_alerts.php',$running_flag);
if (count($running_flag) > 1) {
//print_r ($running_flag);
exit;
}
# Record Start Time
list($micro,$seconds) = split(" ", microtime());
$start = $seconds;
# Store last time running values.
$last_running = mysql_query("SELECT * FROM program_values WHERE status_sweep = 'Push Alerts'") or die ('Querry failed: ' . mysql_error());
# If no values, insert actuals.
if (mysql_num_rows($last_running) == 0){
echo "First time...\n ";
$next = $start + 300;
$insert_push_alert = mysql_query("INSERT INTO program_values (date_next, date_prev, status_sweep) VALUES ('0', '0', 'Push Alerts')") or die ('Querry failed: ' . mysql_error());
$last_running = mysql_query("SELECT * FROM program_values WHERE status_sweep = 'Push Alerts'") or die ('Querry failed: ' . mysql_error());
$available_plugins = dirList(dirname(__FILE__) . "/plugins");
foreach ($available_plugins as $plugin){
system("php '" . dirname(__FILE__) . "/plugins/$plugin'");
}
exit;
}
$available_plugins = dirList(dirname(__FILE__) . "/plugins");
foreach ($available_plugins as $plugin){
system("php '" . dirname(__FILE__) . "/plugins/$plugin'");
}
$last_running_data = mysql_fetch_assoc($last_running);
#echo "$start\n";
# Store scans to be checked for alarms.
$scans = mysql_query("SELECT scan_rec_id FROM scans WHERE ($last_running_data[date_prev] < d_scan ) ORDER BY scan_rec_id ASC") or die ('Querry failed: ' . mysql_error());
#$scans = mysql_query("SELECT scan_rec_id FROM scans WHERE scan_rec_id = 32150") or die ('Querry failed: ' . mysql_error());
$plugins_enabled = mysql_query("SELECT plugin_name FROM plugins WHERE status = 'on' ") or die ('Querry failed: ' . mysql_error());
echo "Processing scans: ".date("D M j G:i:s T Y")."\n";
while ($scan = mysql_fetch_assoc($scans)){
$query_checks_done = mysql_query("SELECT number_of_alerts FROM scans WHERE scan_rec_id = $scan[scan_rec_id]") or die ('Querry failed: ' . mysql_error());
list($result_checks_done) = mysql_fetch_array($query_checks_done);
if ($result_checks_done == 0) {
if (mysql_num_rows($plugins_enabled) > 0){
#$plugins = mysql_fetch_row($plugins_enabled);
echo "\nProcessing alert plugins for scan_rec_id: $scan[scan_rec_id]\n";
while ($plugin = mysql_fetch_assoc($plugins_enabled)) {
system("php '" . dirname(__FILE__) . "/plugins/$plugin[plugin_name].php' $scan[scan_rec_id]");
}
$alerts_to_mail = get_info_mail($scan["scan_rec_id"]);
mysql_query("UPDATE scans SET number_of_alerts = (SELECT COUNT(*) FROM alerts WHERE last_scan_id = $scan[scan_rec_id]) WHERE scan_rec_id = $scan[scan_rec_id]") or die ('Querry failed: ' . mysql_error());
#print_r ($alerts_to_mail);
send_alerts_mail($alerts_to_mail, $scan["scan_rec_id"]);
# Move the the pointer to the begining of the array.
mysql_data_seek($plugins_enabled, 0);
}
}
}
echo "End.\n";
$next = $start + 300;
# Store last time running values.
mysql_query("UPDATE program_values SET date_next = $next, date_prev = $start WHERE status_sweep = 'Push Alerts'") or die ('Querry failed: ' . mysql_error());
?>