Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- root@home:/etc/cron.d $ cat /usr/local/sbin/abnormal.php
- #!/usr/bin/env php
- <?php
- require_once "DB.php";
- /******************************
- * Begin User Configuration
- *****************************/
- $email = "andy@lauppe.org";
- // If you would like a daily report, set this to true, otherwise, only email if percent thresholds are reached
- $daily_report = true;
- // Set each of these percentage thresholds. If they are met, you will receive a report of the abnormal volume of calls or total duration of calls
- $normal_outbound_calls_threshold = "20%";
- $normal_outbound_duration_threshold = "20%";
- $international_outbound_calls_threshold = "20%";
- $international_outbound_duration_threshold = "20%";
- // If you set a unique hostname on each of your PBXs, you do not need to change the $hostname variable below. It will automatically use your
- // system hostname. Otherwise, you should change this to something unique so you know where the emails are coming from ;)
- $hostname = "";
- /******************************
- * End User Configuration
- *****************************/
- function parse_amportal_conf($filename) {
- $file = file($filename);
- foreach ($file as $line) {
- if (preg_match("/^\s*([a-zA-Z0-9]+)\s*=\s*(.*)\s*([;#].*)?/",$line,$matches)) {
- $conf[ $matches[1] ] = $matches[2];
- }
- }
- return $conf;
- }
- if(!$hostname) {
- $hostname = `hostname`;
- }
- $n_vol_theshold = ereg_replace("[^0-9]","",$normal_outbound_calls_threshold)/100;
- $n_dur_theshold = ereg_replace("[^0-9]","",$normal_outbound_duration_threshold)/100;
- $i_vol_theshold = ereg_replace("[^0-9]","",$international_outbound_calls_threshold)/100;
- $i_dur_theshold = ereg_replace("[^0-9]","",$international_outbound_duration_threshold)/100;
- $config = parse_amportal_conf("/etc/amportal.conf");
- $engine = $config['AMPDBENGINE'];
- $db_username = $config['AMPDBUSER'];
- $db_password = $config['AMPDBPASS'];
- $db_host = $config['AMPDBHOST'];
- $db_database = "asteriskcdrdb";
- $db_url = $engine."://".$db_username.":".$db_password."@".$db_host."/".$db_database;
- $db = DB::connect($db_url);
- $email_report = false;
- $sql = "SELECT
- ROUND(AVG(sum_duration))
- FROM (
- SELECT
- SUM(duration)/60 AS sum_duration
- FROM
- cdr
- WHERE
- dst REGEXP '^[0-9]{7,}' AND
- DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= DATE(calldate) AND
- (WEEKDAY(calldate) >= 0 AND WEEKDAY(calldate) <= 4 )
- GROUP BY
- DATE(calldate)
- )
- AS sum_query";
- $result = $db->query($sql);
- list($n_avg_duration) = $result->fetchRow(DB_FETCHMODE_ARRAY);
- $sql = "SELECT ROUND(SUM(duration)/60) FROM cdr WHERE dst REGEXP '^[0-9]{7,}' AND DATE(calldate) >= DATE_SUB(CURDATE(),INTERVAL 1 DAY)";
- $result = $db->query($sql);
- list($n_last_duration) = $result->fetchRow(DB_FETCHMODE_ARRAY);
- $n_duration_percent_change = ($n_last_duration/$n_avg_duration)-1;
- $message = "\n\n";
- if($n_duration_percent_change >= $n_dur_theshold) {
- $email_report = true;
- $message .= "WARNING: ";
- }
- $message .= "Total daily call duration on a week day (average last 30 days): $n_avg_duration minutes. ";
- $message .= "Total call duration for last 24 hours: $n_last_duration minutes. Percent change: ".($n_duration_percent_change*100)."%";
- $sql = "SELECT
- ROUND(AVG(count_calls))
- FROM (
- SELECT
- COUNT(*) as count_calls
- FROM
- cdr
- WHERE
- dst REGEXP '^[0-9]{7,}' AND
- DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= DATE(calldate) AND
- (WEEKDAY(calldate) >= 0 AND WEEKDAY(calldate) <= 4 )
- GROUP BY
- DATE(calldate)
- )
- AS count_query";
- $result = $db->query($sql);
- list($n_avg_total_calls) = $result->fetchRow(DB_FETCHMODE_ARRAY);
- $sql = "SELECT COUNT(*) FROM cdr WHERE dst REGEXP '^[0-9]{7,}' AND DATE(calldate) >= DATE_SUB(CURDATE(),INTERVAL 1 DAY)";
- $result = $db->query($sql);
- list($n_last_total_calls) = $result->fetchRow(DB_FETCHMODE_ARRAY);
- $n_total_calls_percent_change = ($n_last_total_calls/$n_avg_total_calls)-1;
- $message .= "\n\n";
- if($n_total_calls_percent_change >= $n_vol_theshold) {
- $email_report = true;
- $message .= "WARNING: ";
- }
- $message .= "Total oubound calls on a week day (average last 30 days): $n_avg_total_calls. ";
- $message .= "Total outbound calls last 24 hours: $n_last_total_calls. Percent change: ".($n_total_calls_percent_change*100)."%";
- // International
- $sql = "SELECT
- ROUND(AVG(sum_duration))
- FROM (
- SELECT
- SUM(duration)/60 AS sum_duration
- FROM
- cdr
- WHERE
- dst REGEXP '^(00|011)[0-9]{10,}' AND
- DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= DATE(calldate) AND
- (WEEKDAY(calldate) >= 0 AND WEEKDAY(calldate) <= 4 )
- GROUP BY
- DATE(calldate)
- )
- AS sum_query";
- $result = $db->query($sql);
- list($i_avg_duration) = $result->fetchRow(DB_FETCHMODE_ARRAY);
- $sql = "SELECT ROUND(SUM(duration)/60) FROM cdr WHERE dst REGEXP '^(00|011)[0-9]{10,}' AND DATE(calldate) >= DATE_SUB(CURDATE(),INTERVAL 1 DAY)";
- $result = $db->query($sql);
- list($i_last_duration) = $result->fetchRow(DB_FETCHMODE_ARRAY);
- if(!$i_last_duration) {
- $i_last_duration = 0;
- }
- if($i_avg_duration && $i_avg_duration != 0) {
- $i_duration_percent_change = ($i_last_duration/$i_avg_duration)-1;
- }
- else {
- $i_duration_percent_change = 0;
- $i_avg_duration = 0;
- }
- $message .= "\n\n";
- if($i_duration_percent_change >= $i_dur_theshold) {
- $email_report = true;
- $message .= "WARNING: ";
- }
- $message .= "Total international calls duration on a week day (average last 30 days): $i_avg_duration minutes. ";
- $message .= "International calls duration for last 24 hours: $i_last_duration minutes. Percent change: ".($i_duration_percent_change*100)."%";
- $sql = "SELECT
- ROUND(AVG(count_calls))
- FROM (
- SELECT
- COUNT(*) as count_calls
- FROM
- cdr
- WHERE
- dst REGEXP '^(00|011)[0-9]{10,}' AND
- DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= DATE(calldate) AND
- (WEEKDAY(calldate) >= 0 AND
- WEEKDAY(calldate) <= 4 )
- GROUP BY
- DATE(calldate)
- )
- AS count_query";
- $result = $db->query($sql);
- list($i_avg_total_calls) = $result->fetchRow(DB_FETCHMODE_ARRAY);
- if(!$i_avg_total_calls) {
- $i_avg_total_calls = 0;
- }
- $sql = "SELECT COUNT(*) FROM cdr WHERE dst REGEXP '^(00|011)[0-9]{10,}' AND DATE(calldate) >= DATE_SUB(CURDATE(),INTERVAL 1 DAY)";
- $result = $db->query($sql);
- list($i_last_total_calls) = $result->fetchRow(DB_FETCHMODE_ARRAY);
- if($i_avg_total_calls && $i_avg_total_calls != 0) {
- $i_total_calls_percent_change = ($i_last_total_calls/$i_avg_total_calls)-1;
- }
- else {
- $i_total_calls_percent_change = 0;
- $i_avg_total_calls = 0;
- }
- $message .= "\n\n";
- if($i_total_calls_percent_change >= $i_vol_theshold) {
- $email_report = true;
- $message .= "WARNING: ";
- }
- $message .= "Total outbound international calls on a week day (average last 30 days): $i_avg_total_calls. ";
- $message .= "Total outbound international calls 24 hours: $i_last_total_calls. Percent change: ".($i_total_calls_percent_change*100)."%";
- if($daily_report || $email_report) {
- $subject = "Daily call volume report for $hostname";
- $from = "CALL VOLUME REPORT <$email>";
- $headers = "From: $from" . "\r\n" .
- 'Reply-To: noreply@pbx' . "\r\n";
- mail($email,$subject,$message,$headers);
- }
- echo $message;
- ?>
Add Comment
Please, Sign In to add comment