root/experimental_v2/Cronjobs/a2billing_alarm.php

Revision 585, 8.4 kB (checked in by stavros, 1 year ago)

UPDATE: small refinements to alarm emails. Moved the fencepost: they no longer trigger until the metric is outside the defined range.

  • Property svn:executable set to *
Line 
1 #!/usr/bin/php -q
2 <?php
3 /***************************************************************************
4  *            a2billing_alarm.php
5  *
6  *  ADD THIS SCRIPT IN A CRONTAB JOB
7  *
8     crontab -e
9     0 * * * * php /var/lib/asterisk/agi-bin/libs_a2billing/crontjob/a2billing_alarm.php
10     
11     field     allowed values
12     -----     --------------
13     minute             0-59
14     hour             0-23
15     day of month    1-31
16     month             1-12 (or names, see below)
17     day of week         0-7 (0 or 7 is Sun, or use names)
18     
19     
20 ****************************************************************************/
21
22 set_time_limit(0);
23 error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
24 //dl("pgsql.so"); // remove "extension= pgsql.so !
25
26 include_once (dirname(__FILE__)."/lib/Class.Table.php");
27 include (dirname(__FILE__)."/lib/Class.A2Billing.php");
28 include (dirname(__FILE__)."/lib/Misc.php");
29
30
31 $verbose_level=0;
32
33 $groupcard=5000;
34
35 $A2B = new A2Billing();
36 $A2B -> load_conf($agi, NULL, 0, $idconfig);
37
38 write_log(LOGFILE_CRONT_ALARM, basename(__FILE__).' line:'.__LINE__."[#### BATCH BEGIN ####]");
39
40 if (!$A2B -> DbConnect()){               
41     echo "[Cannot connect to the database]\n";
42     write_log(LOGFILE_CRONT_ALARM, basename(__FILE__).' line:'.__LINE__."[Cannot connect to the database]");
43     exit;
44 }
45 //$A2B -> DBHandle
46 $instance_table = new Table();
47
48
49 // CHECK THE ALARMS
50 $QUERY = "SELECT id, name, periode, type, maxvalue, minvalue, id_trunk, status, numberofrun, datecreate,
51 datelastrun, emailreport FROM cc_alarm WHERE status=1";
52
53 $result = $instance_table -> SQLExec ($A2B -> DBHandle, $QUERY);
54
55 if ($verbose_level>=1) print_r ($result);
56
57 if( !is_array($result)) {
58     echo "[No Alarm to run]\n";
59     write_log(LOGFILE_CRONT_ALARM, basename(__FILE__).' line:'.__LINE__."[ No Alarm to run]");
60     exit();
61 }
62 // 0 id, 1 name, 2 period, 3 type, 4 maxvalue, 5 minvalue, 6 id_trunk, 7 status, 8 numberofrun, 9 datecreate, 10 datelastrun, 11 emailreport
63
64
65 $oneday = 60*60*24;
66 $groupcalls = 5000;
67
68 // BROWSE THROUGH THE ALARMS
69 foreach ($result as $myalarm) {
70     $SQL_CLAUSE="";
71     $timestamp_lastsend = strtotime($myalarm[10]);
72     if ($verbose_level>=3) echo "timestamp_lastsend = $timestamp_lastsend"." ; now = ".time();
73     
74     //  1 "Daily", 2 "Weekly", 3 "Monthly"
75     $run_alarm=false;
76     
77     // LITTLE TRICK TO MAKE IT WORKS WITH POSTGRES TOO AS THE VALUE WILL BE NULL
78     if ($myalarm[10] == "0000-00-00 00:00:00"){
79         $myalarm[10] = "";
80         // WE WILL THEN COMPARE TO AN EMPTY STRING ""
81     }
82     
83     switch ($myalarm[2]) {
84         // Hourly
85         case 1:   
86             if (date("G",time())!=date("G",$timestamp_lastsend) || $myalarm[10] == ""){
87                 $run_alarm=true;
88                 $SQL_CLAUSE="WHERE starttime BETWEEN '".date("Y-m-j H:i:s",$timestamp_lastsend)."' AND '".date("Y-m-j H:i:s",time())."'";
89             }
90             if ($verbose_level>=1) echo "\n\n TODAY :".date("G",time())." LAST RUN DAY :".date("G",$timestamp_lastsend);
91         break;
92         // Daily
93         case 2:   
94             if (date("j",time())!=date("j",$timestamp_lastsend) || $myalarm[10] == ""){
95                 $run_alarm=true;
96                 $SQL_CLAUSE="WHERE starttime BETWEEN '".date("Y-m-j",time())." 00:00:00' AND '".date("Y-m-j",time())." 23:59:59'";
97             }
98             if ($verbose_level>=1) echo "\n\n TODAY :".date("j",time())." LAST RUN DAY :".date("j",$timestamp_lastsend);
99         break;
100         //Weekly -> will run only monday and check if the week is not the same
101         case 3:
102             if (((date("w",time()) == 1) && (date("W",time())!=date("W",$timestamp_lastsend))) || $myalarm[10] == ""){
103                 $run_alarm=true;
104                 $SQL_CLAUSE="WHERE starttime BETWEEN '".date("Y-m-j",(time()-($oneday*7)))." 00:00:00' AND '".date("Y-m-j",time())." 23:59:59'";
105             }
106             if ($verbose_level>=1) echo "\n\n TODAY :".date("w",time())." WEEK:".date("W",time()).
107                                         " LAST RUN DAY :".date("w",$timestamp_lastsend)." LAST RUN WEEK :".date("W",$timestamp_lastsend);
108         break;
109         //Monthly
110         case 4:
111             if (((date("j",time()) == 1) && (date("m",time())!=date("m",$timestamp_lastsend))) || $myalarm[10] == ""){
112                 $run_alarm=true;
113                 $SQL_CLAUSE="WHERE starttime BETWEEN '".date("Y-m-j",(time()-($oneday*date("t",time()-$oneday))))." 00:00:00' AND '".date("Y-m-j",(time()-$oneday))." 23:59:59'";
114             }
115             if ($verbose_level>=1) echo "\n\n THIS MONTH :".date("m",time())." LAST RUN MONTH :".date("m",$timestamp_lastsend);
116         break;
117     }
118     
119     if ($run_alarm){
120         
121         if (isset($myalarm[6]) && $myalarm[6] != "") $SQL_CLAUSE.=" AND id_trunk = '".$myalarm[6]."'";
122         write_log(LOGFILE_CRONT_ALARM, basename(__FILE__).' line:'.__LINE__."[Alarm : ".$myalarm[1]." ]");
123
124         $QUERY =    "SELECT COUNT(*) FROM cc_call $SQL_CLAUSE";
125         if ($verbose_level>=1) echo "\n===> QUERY = $QUERY\n";
126         $calls = $instance_table -> SQLExec ($A2B -> DBHandle, $QUERY,1);
127         $nb_card = $calls[0][0];
128         $nbpagemax=(intval($nb_card/$groupcalls));
129         if ($verbose_level>=1) echo "===> NB_CARD : $nb_card - NBPAGEMAX:$nbpagemax\n";
130         
131         if (!($nb_card>0)){
132             if ($verbose_level>=1) echo "[No call to run the Alarm Service]\n";
133                 write_log(LOGFILE_CRONT_ALARM, basename(__FILE__).' line:'.__LINE__."[No call to run the Alarm service]");
134                 exit();
135         }
136         
137         $totalsuccess=0;
138         $totalfail=0;
139         $totaltime=0;
140         $max_fail=0;
141         $max=0;
142         $update=array();
143         for ($page = 0; $page <= $nbpagemax; $page++)
144         {
145             // REST AFTER $groupcalls CARD HANDLED
146             if ($page > 0) sleep(15);
147             
148             $QUERY =    "SELECT CASE WHEN (terminatecause = 'ANSWER' OR terminatecause = 'ANSWERED') THEN 1 ELSE 0 END AS success,".
149                         "CASE WHEN (terminatecause = 'ANSWER' OR terminatecause = 'ANSWERED') THEN 0 ELSE 1 END AS fail,sessiontime FROM cc_call $SQL_CLAUSE";
150             
151             if ($A2B->config["database"]['dbtype'] == "postgres")
152             {
153                 $QUERY .= " LIMIT $groupcard OFFSET ".$page*$groupcard;
154             }
155             else
156             {
157                 $QUERY .= " LIMIT ".$page*$groupcard.", $groupcard";
158             }
159             
160             if ($verbose_level>=1) echo "\n\n".$QUERY;
161             
162             $res = $instance_table -> SQLExec ($A2B -> DBHandle, $QUERY,1);
163             for($i=0;$i<count($res);$i++)
164             {
165                 $totalsuccess+=$res[$i][0];
166                 $totalfail+=$res[$i][1];
167                 $totaltime+=$res[$i][2];
168                 // FIND THE CIC (Consecutive Incomplete Calls)
169                 if($res[$i][1] == 1)    $max++;   
170                 if($res[$i][1] == 0)
171                 {
172                     if($max > $max_fail)
173                     $max_fail = $max;
174                     $max = 0;
175                 }
176             }
177             if($max > $max_fail)    $max_fail = $max;
178             
179         } // LOOP FOR THE CALLS
180     
181         
182         if ($max_fail == 1)    $max_fail = 0;
183     
184         $ASR = $totalsuccess / ($totalsuccess + $totalfail);
185         $ALOC = $totaltime / ($totalsuccess + $totalfail);
186         
187         $send_alarm = false;
188         
189         //$type_list   ["0" ALOC, "1" ASR, "2" CIC]
190         switch ($myalarm[3]) {
191             case 1:
192                 if ( $ALOC > $myalarm[4] || $ALOC < $myalarm[5]){
193                     $value=$ALOC;
194                     $send_alarm=true;
195                 }
196                 $content="\n\n The ALOC $ALOC seconds is outside the range max: ".$myalarm[4]." min:".$myalarm[5]." defined in the alarm";
197             break;
198             case 2:
199                 if ($ASR > $myalarm[4] || $ASR < $myalarm[5]){
200                     $value = $ASR;
201                     $send_alarm=true;
202                 }
203                 $content="\n\n The ASR ".$ASR."% is outside the range max: ".$myalarm[4]." min:".$myalarm[5]." defined in the alarm";
204             break;
205             
206             case 3:
207                 if ($max_fail >= $myalarm[4]){   
208                     $value=$max_fail;
209                     $send_alarm=true;
210                 }
211                 $content="\n\n The Max Consecutive Incomplete Calls : ".$max_fail." calls is greater than the max: ".$myalarm[4]." defined in the alarm";
212             break;
213         }
214         if ($verbose_level>=1) echo "content = $content\n";
215         write_log(LOGFILE_CRONT_ALARM, basename(__FILE__).' line:'.__LINE__."[Alarm finish]");
216         // INSERT REPORT ALARM INTO THE DATABASE
217         $QUERY = "INSERT INTO cc_alarm_report (cc_alarm_id, calculatedvalue, daterun) ". "VALUES ('$myalarm[0]', '$value', now())";
218         $result_insert = $instance_table -> SQLExec ($A2B -> DBHandle, $QUERY, 0);
219         if ($verbose_level>=1) echo "\n\n ==> INSERT ALARM REPORT QUERY=$QUERY\n";
220         
221         write_log(LOGFILE_CRONT_ALARM, basename(__FILE__).' line:'.__LINE__."[Alarm report : 'Alarm name=$myalarm[1]', 'Alarm type=$myalarm[3]', 'Calculated value=$value']");
222         
223         // UPDATE THE ALARM
224         if ($send_alarm) $add_field=", numberofalarm=numberofalarm+1";
225         else $add_field="";
226         $QUERY = "UPDATE cc_alarm SET datelastrun=now(), numberofrun=numberofrun+1 $add_field WHERE id=".$myalarm[0];
227         $result = $instance_table -> SQLExec ($A2B -> DBHandle, $QUERY, 0);
228         if ($verbose_level>=1) echo "\n\n ==> ALARM UPDATE QUERY:     $QUERY\n";
229         
230         
231         // SEND REPORT
232         if (($send_alarm) && (strlen($myalarm[7])>0)){
233             $mail_content = "ALARM NAME = ".$myalarm[1];
234             $mail_content .= $content;
235             mail($myalarm[11], "A2BILLING ALARM : REPORT", $mail_content);
236         }
237         
238     } // IF ALARM
239     
240 } // MAIN LOOP FOR THE ALARM
241
242 if ($verbose_level>=1) echo "#### END ALARMS \n";
243 write_log(LOGFILE_CRONT_ALARM, basename(__FILE__).' line:'.__LINE__."[#### ALARM END ####]");
244
245
246
247 ?>
248
Note: See TracBrowser for help on using the browser.


Google