uses('getconf,functions'); $smtpin_ips = gethostbynamel($hostname); $smtpin_ips_v6 = $app->functions->gethostbynamel6($hostname); if ($smtpin_ips_v6) { $smtpin_ips = array_merge($smtpin_ips, $smtpin_ips_v6); } return $smtpin_ips; } public function onRunJob() { global $app, $conf; $app->uses('getconf,functions'); $mail_config = $app->getconf->get_server_config($conf['server_id'], 'mail'); /* used for all monitor cronjobs */ $app->load('monitor_tools'); $this->_tools = new monitor_tools(); /* end global section for monitor cronjobs */ // Initialize data array $data = array(); $state = 'no_state'; // the id of the server as int $server_id = intval($conf['server_id']); $hostname = $app->system->hostname(); $smtpin_ips = $this->_resolveHostnameBoth46($hostname); # Add additional IP's, e.g. an extrernal spamfilter/proxy. if (!empty($mail_config['additional_smtp_hostnames'])) { $additional_smtp_hostnames = explode(',', $mail_config['additional_smtp_hostnames']); foreach ($additional_smtp_hostnames as $hostname) { $extra = $this->_resolveHostnameBoth46($hostname); if ($extra) { $smtpin_ips = array_merge($smtpin_ips, $extra); } } } # Add additional IP's , e.g. for secondary IP on the same box or a proxy. if (!empty($mail_config['additional_smtp_ips'])) { $smtpin_ips = array_merge($smtpin_ips, explode(',', $mail_config['additional_smtp_ips'])); } $maildomains = $app->db->queryAllRecords("SELECT domain, active FROM mail_domain WHERE server_id = ?", $server_id); if(is_array($maildomains)) { $state = 'ok'; foreach ($maildomains as $maildomain) { $mx_records = array(); $mx_weight = array(); $found_mx = getmxrr($maildomain['domain'], $mx_records, $mx_weight) ; $mx_sorted = array(); // Merge records and weight into a single array to sort on priority. // ignore multiple mx's at the same weight foreach ($mx_records as $key => $name) { $mx_sorted[$mx_weight[$key]] = $mx_records[$key]; } ksort ($mx_sorted, SORT_NUMERIC); reset ($mx_sorted); $first_mx = array_shift($mx_sorted); $mx_ip = gethostbyname($first_mx); if (!in_array( $mx_ip, $smtpin_ips)) { if ($maildomain['active'] == 'y') { $str = 'Domain is active but the DNS does not match our IP. ( points to: ' . ($first_mx ?? 'not found') . ')'; $app->log('Mail domain[' . $maildomain['domain'] . ']: ' . $str, LOGLEVEL_WARN); $state = 'warning'; $data[$maildomain['domain']] = $str; } else { $app->log('Good, the mail domain[' . $maildomain['domain'] . '] is not active and DNS is not pointing to us.', LOGLEVEL_DEBUG); } } else { if ($maildomain['active'] == 'n') { $app->log('DNS points to our IP but the mail domain[' . $maildomain['domain'] . '] is not active.', LOGLEVEL_WARN); $state = 'warning'; $data[$maildomain['domain']] = 'DNS points to our IP but the mail domain is not active.'; } else { // DNS OK. } } } } $res = array(); $res['server_id'] = $server_id; $res['type'] = 'mx_ip_match'; $res['data'] = $data; $res['state'] = $state; /** * Insert the data into the database */ $sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' . 'VALUES (?, ?, UNIX_TIMESTAMP(), ?, ?)'; $app->dbmaster->query($sql, $res['server_id'], $res['type'], serialize($res['data']), $res['state']); // The new data is written, now we can delete the old one. $this->_tools->delOldRecords($res['type'], $res['server_id']); parent::onRunJob(); } /* this function is optional if it contains no custom code */ public function onAfterRun() { global $app; parent::onAfterRun(); } } ?>