public function __construct()
 {
     if (self::$instance === null) {
         if (!file_exists('/etc/blacklistmonitor.cfg')) {
             echo 'no config file in /etc/blacklistmonitor.cfg';
         }
         ini_set('error_reporting', E_ALL | E_STRICT | E_NOTICE);
         $cfg = parse_ini_file('/etc/blacklistmonitor.cfg', false);
         ini_set('display_errors', $cfg['display_errors']);
         ini_set('error_log', $cfg['log_path']);
         self::$settings = $cfg;
         self::$settings['dns_servers'] = explode(',', $cfg['dns_servers']);
         self::$connectionArray = array($cfg['db_host'], $cfg['db_username'], $cfg['db_password'], $cfg['db_database']);
         _Logging::$logFileLocation = $cfg['log_path'];
     }
 }
 public static function getAccount()
 {
     $mysql = new _MySQL();
     $mysql->connect(Setup::$connectionArray);
     $ret = false;
     $rs = $mysql->runQuery("\n\t\t\tselect\n\t\t\t\tusername,\n\t\t\t\tpasswd,\n\t\t\t\tapiKey,\n\t\t\t\tbeenChecked,\n\t\t\t\tdisableEmailNotices,\n\t\t\t\tnoticeEmailAddresses,\n\t\t\t\ttextMessageEmails,\n\t\t\t\ttwitterHandle,\n\t\t\t\tapiCallbackURL,\n\t\t\t\tcheckFrequency\n\t\t\tfrom users limit 1");
     while ($row = mysqli_fetch_array($rs, MYSQL_ASSOC)) {
         $ret = $row;
     }
     $mysql->close();
     if (!$ret) {
         //account
         _Logging::appLog("no user account");
         exit;
     }
     return $ret;
 }
$mysql = new _MySQL();
$mysql->connect(Setup::$connectionArray);
$rs = $mysql->runQuery("\n\tselect *\n\tfrom monitors\n\twhere ipDomain = '" . $mysql->escape($options['h']) . "'");
while ($row = mysqli_fetch_array($rs, MYSQL_ASSOC)) {
    $monitor = $row;
}
// get blacklists
Utilities::setBlockLists();
if (empty(Utilities::$domainBlacklists) === true && empty(Utilities::$ipBlacklists) === true) {
    _Logging::appLog("no blacklists configured");
    exit;
}
//update monitor
$result = serialize(Utilities::checkBlacklists($monitor['ipDomain']));
$isBlocked = Utilities::$isBlocked;
$rdns = Utilities::lookupHostDNS($monitor['ipDomain']);
$ctime = date('Y-m-d H:i:s');
$mysql->runQuery("\nupdate monitors\nset\nlastStatusChanged = 0,\nrDNS = '" . $mysql->escape($rdns) . "', \nisBlocked = {$isBlocked},\nlastUpdate = '{$ctime}', \nstatus = '" . $mysql->escape($result) . "' \nwhere ipDomain = '" . $mysql->escape($monitor['ipDomain']) . "'\n");
// status change on this host
if (strcasecmp($result, $monitor['status']) != 0) {
    //update current status
    $mysql->runQuery("\n\t\tupdate monitors\n\t\tset\n\t\t\tlastStatusChanged = 1,\n\t\t\tlastStatusChangeTime = '" . date('Y-m-d H:i:s') . "'\n\t\twhere ipDomain = '" . $mysql->escape($monitor['ipDomain']) . "'\n\t\t");
    //log history
    $mysql->runQuery("\n\t\tinsert into monitorHistory\n\t\t(monitorTime, isBlocked, ipDomain, rDNS, status)\n\t\tvalues(\n\t\t'" . date('Y-m-d H:i:s') . "',\n\t\t{$isBlocked},\n\t\t'" . $mysql->escape($monitor['ipDomain']) . "',\n\t\t'" . $mysql->escape($rdns) . "',\n\t\t'" . $mysql->escape($result) . "')");
    //make api callback
    $user = Utilities::getAccount();
    if ($user['apiCallbackURL'] != '') {
        Utilities::makeAPICallback($user['apiCallbackURL'], $monitor['ipDomain'], $isBlocked, $rdns, $result);
        _Logging::appLog("api callback made: {$user['apiCallbackURL']}");
    }
}
Beispiel #4
0
        $mail->Subject = Setup::$settings['alert_subject'];
        $mail->isHtml(true);
        $mail->Body = "{$noticeMessage} {$summary} {$table} {$footer}";
        $mail->Send();
    }
    // text message
    $e = explode("\n", $user['textMessageEmails']);
    if (count($e) > 0 && Setup::$settings['smtp_server'] != '') {
        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->Host = Setup::$settings['smtp_server'];
        $mail->From = Setup::$settings['from_email'];
        $mail->FromName = Setup::$settings['from_name'];
        foreach ($e as $a) {
            if (trim($a) != '') {
                $mail->AddAddress($a);
            }
        }
        $mail->Subject = Setup::$settings['alert_subject_sms'];
        $mail->isHtml(false);
        $mail->Body = "{$url}/hosts.php?oc=1 {$summaryText}";
        $mail->Send();
    }
    if ($user['twitterHandle'] != '') {
        $t = new Twitter();
        $t->message($user['twitterHandle'], $summaryText);
    }
    _Logging::appLog("user alert sent");
}
_Logging::appLog("user job ended");
function monitorProcessWatch($parentProcessId)
{
    $m = new _MeasurePerformance();
    $mysql = new _MySQL();
    $mysql->connect(Setup::$connectionArray);
    $parallelProcessesMonitors = Setup::$settings['max_monitor_processes'];
    $monitorProcesses = array();
    $processCountMonitors = 0;
    $ipDomain = false;
    while (true) {
        // are we still running?
        if (!Utilities::is_process_running($parentProcessId)) {
            _Logging::appLog("Parent Stopped - monitorStartWatch exited");
            exit;
        }
        $processCountMonitors = count($monitorProcesses);
        if ($processCountMonitors < $parallelProcessesMonitors) {
            $ipDomain = Utilities::getNextMonitor($mysql);
            if ($ipDomain !== false) {
                // start it
                $cmd = 'php ' . dirname(__FILE__) . '/monitorJob.php -h ' . escapeshellarg($ipDomain);
                $pid = Utilities::run_in_background($cmd);
                $m->work(1);
                $monitorProcesses[] = $pid;
            }
        }
        // was there any work?
        if ($ipDomain === false) {
            sleep(10);
            //10 seconds
        } else {
            usleep(10000);
            //ideal time 10ms
        }
        // delete finished processes
        for ($x = 0; $x < $processCountMonitors; $x++) {
            if (isset($monitorProcesses[$x])) {
                if (!Utilities::is_process_running($monitorProcesses[$x])) {
                    unset($monitorProcesses[$x]);
                }
            }
        }
        // fix array index
        $monitorProcesses = array_values($monitorProcesses);
        $processCountMonitors = count($monitorProcesses);
        //randomly reset counter every now and then
        if (mt_rand(1, 2000) == 1) {
            $m->endWork();
            _Logging::appLog("App Avg Hosts/sec: {$m->avgPerformance}\tMonitor Threads: {$processCountMonitors}/{$parallelProcessesMonitors}");
            $m = new _MeasurePerformance();
        }
    }
}
Beispiel #6
0
    _Logging::appLog("block list stats updated");
}
if ($options['r'] == 'weekly') {
    $mysql->runQuery("update users set beenChecked = 0 where checkFrequency = 'weekly';");
    _Logging::appLog("weekly reset");
}
if ($options['r'] == 'daily') {
    $mysql->runQuery("update users set beenChecked = 0 where checkFrequency = 'daily';");
    _Logging::appLog("daily reset");
}
if ($options['r'] == '8hour') {
    $mysql->runQuery("update users set beenChecked = 0 where checkFrequency = '8hour';");
    _Logging::appLog("8 hour reset");
}
if ($options['r'] == '2hour') {
    $mysql->runQuery("update users set beenChecked = 0 where checkFrequency = '2hour';");
    _Logging::appLog("2 hour reset");
}
if ($options['r'] == '1hour') {
    $mysql->runQuery("update users set beenChecked = 0 where checkFrequency = '1hour';");
    _Logging::appLog("1 hour reset");
}
if ($options['r'] == 'deleteOld') {
    //clear orphan status
    $mysql->runQuery("\n\t\tdelete mh\n\t\tfrom monitorHistory mh\n\t\tleft join monitors m on m.ipDomain = mh.ipDomain\n\t\twhere m.ipDomain IS NULL\n\t\t");
    $days = isset(Setup::$settings['history_keep_days']) ? (int) Setup::$settings['history_keep_days'] : 0;
    if ($days > 0) {
        $mysql->runQuery("\n\t\t\tdelete from monitorHistory\n\t\t\twhere monitorTime <= DATE_SUB(NOW(), INTERVAL {$days} day)\n\t\t\t");
        _Logging::appLog("old data deleted");
    }
}