~ubuntu-dev/ubuntu/lucid/zabbix/lucid-201002110857

« back to all changes in this revision

Viewing changes to frontends/php/include/services.inc.php

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Haas
  • Date: 2009-10-10 12:21:16 UTC
  • mfrom: (1.1.10 upstream) (8.2.6 sid)
  • Revision ID: james.westby@ubuntu.com-20091010122116-44k2zdq57rb40i6z
Tags: 1:1.6.6-2
* debian/po/ja.po updated (Closes: #548651)
* debian/po/cs.po updated (Closes: #548675)
* debian/po/sv.po updated (Closes: #548796)
* debian/po/de.po updated (Closes: #549248)
* debian/po/it.po updated (Closes: #549579)
* debian/po/pt.po updated (Closes: #550087)
* debian/po/ru.po updated (Closes: #550102)
* debian/po/es.po updated (Closes: #550173)
* debian/po/fr.po updated (Closes: #550315)
* Manpages for zabbix_server and zabbix_agent added (Closes: #496696)
* Added hint about the difference of the zabbix_agent.conf and
  zabbix_agentd.conf in each config file as a comment. (Closes: #548701)

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
?>
21
21
<?php
22
22
        function        add_service($name,$triggerid,$algorithm,$showsla,$goodsla,$sortorder,$service_times=array(),$parentid,$childs){
23
 
        
 
23
 
24
24
                foreach($childs as $id => $child){              //add childs
25
25
                        if((bccomp($parentid , $child['serviceid'])==0)){
26
26
                                error('Service can\'t be parent and child in onetime.');
27
27
                                return FALSE;
28
28
                        }
29
29
                }
30
 
                
 
30
 
31
31
                if(is_null($triggerid) || $triggerid==0) $triggerid = 'NULL';
32
32
 
33
33
                $serviceid=get_dbid("services","serviceid");
34
 
                
 
34
 
35
35
                remove_service_links($serviceid); //removes all links with current serviceid
36
36
 
37
37
                $result =($parentid != 0)?(add_service_link($serviceid,$parentid,0)):(true); //add parent
38
 
                
 
38
 
39
39
                foreach($childs as $id => $child){              //add childs
40
40
                        if(!isset($child['soft']) || empty($child['soft'])) $child['soft'] = 0;
41
 
                        $result = add_service_link($child['serviceid'],$serviceid,$child['soft']); 
 
41
                        $result = add_service_link($child['serviceid'],$serviceid,$child['soft']);
42
42
                }
43
 
                
 
43
 
44
44
                if(!$result){
45
45
                        return FALSE;
46
46
                }
51
51
                if(!$result) return FALSE;
52
52
 
53
53
                update_services_status_all(); // updating status to all services by the dependency
54
 
                
 
54
 
55
55
                DBExecute('DELETE FROM services_times WHERE serviceid='.$serviceid);
56
56
 
57
57
                foreach($service_times as $val){
78
78
                remove_service_links($serviceid); //removes all links with current serviceid
79
79
 
80
80
                $result =($parentid != 0)?(add_service_link($serviceid,$parentid,0)):(true); //add parent
81
 
                
 
81
 
82
82
                foreach($childs as $id => $child){              //add childs
83
83
                        if(empty($child['soft']) || !isset($child['soft'])) $child['soft'] = 0;
84
 
                        $result = add_service_link($child['serviceid'],$serviceid,$child['soft']); 
 
84
                        $result = add_service_link($child['serviceid'],$serviceid,$child['soft']);
85
85
                }
86
 
                
 
86
 
87
87
                if(!$result) return FALSE;
88
 
                
 
88
 
89
89
                if(is_null($triggerid) || $triggerid==0) $triggerid = 'NULL';
90
90
 
91
91
                $result = DBexecute('UPDATE services '.
105
105
 
106
106
                return $result;
107
107
        }
108
 
        
 
108
 
109
109
        function        add_host_to_services($hostid, $serviceid){
110
110
                $result = DBselect('SELECT distinct h.host,t.triggerid,t.description,t.expression '.
111
111
                                                        ' FROM triggers t,hosts h,items i,functions f '.
115
115
                                                                ' AND f.triggerid=t.triggerid '.
116
116
                                                                ' AND '.DBin_node('t.triggerid', get_current_nodeid(false))
117
117
                                                        );
118
 
                                                        
 
118
 
119
119
                while($row=DBfetch($result)){
120
120
                        $serviceid2 = add_service(expand_trigger_description_by_data($row),$row["triggerid"],"on",0,"off",99);
121
121
                        add_service_link($serviceid2,$serviceid,0);
131
131
                }
132
132
                return  FALSE;
133
133
        }
134
 
        
 
134
 
135
135
        /*
136
136
         * Function: get_service_status
137
137
         *
138
 
         * Description: 
 
138
         * Description:
139
139
         *     retrive true status
140
 
         *     
141
 
         * Author: 
 
140
         *
 
141
         * Author:
142
142
         *     Aly
143
143
         *
144
144
         * Comments:
145
145
         *              Don't forget to sync code with C!!!!
146
146
         */
147
 
        
 
147
 
148
148
        function get_service_status($serviceid,$algorithm,$triggerid=null,$status=0){
149
 
                
 
149
 
150
150
                if(is_numeric($triggerid)){
151
151
                        $status = ($serv_status = get_service_status_of_trigger($triggerid))?$serv_status:$status;
152
152
                }
153
153
 
154
154
                if((SERVICE_ALGORITHM_MAX == $algorithm) || (SERVICE_ALGORITHM_MIN == $algorithm)){
155
155
                        $sort_order = (SERVICE_ALGORITHM_MAX == $algorithm)?' DESC ':' ASC ';
156
 
                        
 
156
 
157
157
                        $sql = 'SELECT s.status'.
158
158
                                        ' FROM services s,services_links l '.
159
159
                                        ' WHERE l.serviceupid='.$serviceid.
160
160
                                                ' AND s.serviceid=l.servicedownid '.
161
161
                                        ' ORDER BY s.status '.$sort_order;
162
162
                        $result = DBselect($sql);
163
 
                        
164
 
                        $rows = DBfetch($result);       
 
163
 
 
164
                        $rows = DBfetch($result);
165
165
                        if($rows && !zbx_empty($rows['status'])){
166
166
                                $status=$rows['status'];
167
167
                        }
188
188
                if(!$result=DBexecute($sql)) return     $result;
189
189
 
190
190
                update_services_status_all();
191
 
                
 
191
 
192
192
                return $result;
193
193
        }
194
 
        
 
194
 
195
195
        /*
196
196
         * Function: clear_parents_from_trigger
197
197
         *
198
 
         * Description: 
 
198
         * Description:
199
199
         *     removes any links between trigger and service if service is not leaf (treenode)
200
 
         *     
201
 
         * Author: 
 
200
         *
 
201
         * Author:
202
202
         *     Aly
203
203
         *
204
204
         * Comments:
205
205
         *
206
206
         */
207
 
        
 
207
 
208
208
        function clear_parents_from_trigger($serviceid=0){
209
209
                if($serviceid != 0){
210
210
                        $sql='UPDATE services '.
240
240
                        if($service["status"]>0){
241
241
                                return TRUE;
242
242
                        }
243
 
                        
 
243
 
244
244
                }
245
245
 
246
246
                $result=DBselect("SELECT serviceupid FROM services_links WHERE servicedownid=$serviceid2 and soft=0");
290
290
 
291
291
                return $linkid;
292
292
        }
293
 
        
 
293
 
294
294
        function update_service_link($linkid,$servicedownid,$serviceupid,$softlink){
295
295
                if( ($softlink==0) && (is_service_hardlinked($servicedownid)==true) ){
296
296
                        return  false;
304
304
                $sql="UPDATE services_links SET servicedownid=$servicedownid, serviceupid=$serviceupid, soft=$softlink WHERE linkid=$linkid";
305
305
                return  dbexecute($sql);
306
306
        }
307
 
        
 
307
 
308
308
        function remove_service_links($serviceid){
309
 
                $query='DELETE 
310
 
                                FROM services_links 
311
 
                                WHERE serviceupid='.$serviceid.' 
312
 
                                        OR  (servicedownid='.$serviceid.' 
 
309
                $query='DELETE
 
310
                                FROM services_links
 
311
                                WHERE serviceupid='.$serviceid.'
 
312
                                        OR  (servicedownid='.$serviceid.'
313
313
                                        AND soft<>1)';
314
314
                DBExecute($query);
315
315
        }
316
 
        
 
316
 
317
317
        function get_last_service_value($serviceid,$clock){
318
318
                $sql="SELECT count(*) as cnt,max(clock) as maxx FROM service_alarms WHERE serviceid=$serviceid and clock<=$clock";
319
319
//              echo " $sql<br>";
320
 
                
 
320
 
321
321
                $result=DBselect($sql);
322
322
                $row=DBfetch($result);
323
323
                if($row["cnt"]>0){
334
334
                }
335
335
        return $value;
336
336
        }
337
 
/*      
 
337
/*
338
338
function VDI($time,$show=1){
339
339
        $time = (is_array($time))?$time:getdate($time);
340
340
return ($time['mon'].'/'.$time['mday'].'/'.$time['year'].' '.$time['hours'].':'.$time['minutes'].':'.$time['seconds']);
344
344
//SDI("PERIOD: ".VDI($period_start).' - '.VDI($period_end));
345
345
//SDI('serv time: '.VDI($ts_from,0).' - '.VDI($ts_to,0));
346
346
                        /* calculate period FROM '-1 week' to know period name for  $period_start */
347
 
                        
 
347
 
348
348
                        for($curr = ($period_start - (7*24*3600)); $curr<=$period_end;$curr+=86400){
349
 
                        
 
349
 
350
350
                                $curr_date = getdate($curr);
351
351
                                $from_date = getdate($ts_from);
352
352
 
414
414
                                                ' FROM services_times '.
415
415
                                                ' WHERE type='.SERVICE_TIME_TYPE_UPTIME.
416
416
                                                        ' AND serviceid='.$serviceid);
417
 
                                                        
 
417
 
418
418
                if($db_time_row = DBfetch($service_times)){
419
419
                        /* if exist any uptime - unmarked time is downtime */
420
420
                        $unmarked_period_type = 'dt';
444
444
                /* add one-time downtimes */
445
445
                $service_times = DBselect('SELECT ts_from,ts_to FROM services_times WHERE type='.SERVICE_TIME_TYPE_ONETIME_DOWNTIME.' and serviceid='.$serviceid);
446
446
                while($db_time_row = DBfetch($service_times)){
447
 
                
 
447
 
448
448
                        if( ($db_time_row['ts_to'] < $period_start) || ($db_time_row['ts_from'] > $period_end)) continue;
449
449
 
450
450
                        if($db_time_row['ts_from'] < $period_start)     $db_time_row['ts_from'] = $period_start;
530
530
 
531
531
                        $prev_time = $ts;
532
532
                }
533
 
                
 
533
 
534
534
/*
535
535
if($serviceid == 1 || $serviceid == 2){
536
536
        SDI($serviceid);
614
614
                }
615
615
                return S_UNKNOWN;
616
616
        }
617
 
        
 
617
 
618
618
        function get_service_childs($serviceid,$soft=0){
619
619
                $childs = array();
620
620
 
621
621
                $query = 'SELECT sl.servicedownid '.
622
622
                        ' FROM services_links sl '.
623
623
                        ' WHERE sl.serviceupid = '.$serviceid.($soft?'':' AND sl.soft <> 1');
624
 
                
 
624
 
625
625
                $res =  DBSelect($query);
626
626
                while($row = DBFetch($res)){
627
627
                        $childs[] = $row['servicedownid'];
629
629
                }
630
630
                return $childs;
631
631
        }
632
 
        
 
632
 
633
633
        function createServiceTree(&$services,&$temp,$id=0,$serviceupid=0,$parentid=0, $soft=0, $linkid=''){
634
634
 
635
635
                $rows = $services[$id];
636
636
                if(($rows['serviceid'] > 0) && ($rows['caption'] != 'root')){
637
637
                        $rows['algorithm'] = algorithm2str($rows['algorithm']);
638
638
                }
639
 
        
 
639
 
640
640
        //---------------------------- if not leaf -----------------------------
641
641
                $rows['parentid'] = $parentid;
642
642
                if($soft == 0){
643
643
                        $rows['caption'] = new CLink($rows['caption'],'#',null,'javascript: call_menu(event, '.zbx_jsvalue($rows['serviceid']).','.zbx_jsvalue($rows['caption']).'); return false;');
644
 
                                
 
644
 
645
645
                        $temp[$rows['serviceid']]=$rows;
646
 
                
 
646
 
647
647
                        if(isset($rows['childs'])){
648
648
                                foreach($rows['childs'] as $cid => $nodeid){
649
649
                                        if(!isset($services[$nodeid['id']])){
652
652
                                        if(isset($services[$nodeid['id']]['serviceupid'])){
653
653
                                                createServiceTree($services,$temp,$nodeid['id'],$services[$nodeid['id']]['serviceupid'],$rows['serviceid'],$nodeid['soft'], $nodeid['linkid']);
654
654
                                        }
655
 
                                }                       
 
655
                                }
656
656
                        }
657
657
                } else {
658
658
                        $rows['caption'] = new CSpan($rows['caption'],'unknown');
660
660
                }
661
661
        return ;
662
662
        }
663
 
        
 
663
 
664
664
        function createShowServiceTree(&$services,&$temp,$id=0,$serviceupid=0,$parentid=0, $soft=0, $linkid=''){
665
665
 
666
666
                $rows = $services[$id];
667
 
                
668
 
        
 
667
 
 
668
 
669
669
        //---------------------------- if not leaf -----------------------------
670
670
                $rows['parentid'] = $parentid;
671
671
                if(($rows['serviceid']  > 0) && ($rows['caption'] != 'root')){
672
672
                        $rows['status'] = get_service_status_description($rows["status"]);
673
673
                }
674
 
                
 
674
 
675
675
                if($soft == 0){
676
676
 
677
677
                        $temp[$rows['serviceid']]=$rows;
678
 
                
 
678
 
679
679
                        if(isset($rows['childs'])){
680
680
                                foreach($rows['childs'] as $cid => $nodeid){
681
681
                                        if(!isset($services[$nodeid['id']])){
683
683
                                        }
684
684
                                        if(isset($services[$nodeid['id']]['serviceupid'])){
685
685
                                                createShowServiceTree($services,$temp,$nodeid['id'],$services[$nodeid['id']]['serviceupid'],$rows['serviceid'],$nodeid['soft'], $nodeid['linkid']);     }
686
 
                                }                       
 
686
                                }
687
687
                        }
688
 
                } 
 
688
                }
689
689
                else {
690
690
                        $rows['caption'] = new CSpan($rows['caption']);
691
691
                        $rows['caption']->AddOption('style','color: #888888;');
693
693
                }
694
694
        return ;
695
695
        }
696
 
                
 
696
 
697
697
        function del_empty_nodes($services){
698
698
                do{
699
699
                        unset($retry);
707
707
                } while(isset($retry));
708
708
        return $services;
709
709
        }
710
 
        
 
710
 
711
711
/******************************************************************************
712
712
 *                                                                            *
713
713
 * Function: update_services_rec                                              *
734
734
        while($rows=DBfetch($result)){
735
735
                $serviceupid = $rows['serviceupid'];
736
736
                $algorithm = $rows['algorithm'];
737
 
                
 
737
 
738
738
                if(SERVICE_ALGORITHM_NONE == $algorithm){
739
739
                        /* Do nothing */
740
740
                }
741
741
                else if((SERVICE_ALGORITHM_MAX == $algorithm) || (SERVICE_ALGORITHM_MIN == $algorithm)){
742
742
 
743
743
                        $status = get_service_status($serviceupid,$algorithm);
744
 
                        
 
744
 
745
745
                        add_service_alarm($serviceupid,$status,time());
746
746
                        DBexecute('UPDATE services SET status='.$status.' WHERE serviceid='.$serviceupid);
747
747
                }
778
778
 ******************************************************************************/
779
779
function update_services($triggerid, $status){
780
780
        DBexecute('UPDATE services SET status='.$status.' WHERE triggerid='.$triggerid);
781
 
        
 
781
 
782
782
        $result = DBselect('SELECT serviceid,algorithm FROM services WHERE triggerid='.$triggerid);
783
783
 
784
784
        while(($rows=DBfetch($result))){
792
792
/*
793
793
 * Function: update_services_status_all
794
794
 *
795
 
 * Description: 
 
795
 * Description:
796
796
 *     Cleaning parent nodes from triggers, updating ALL services status.
797
 
 *     
798
 
 * Author: 
 
797
 *
 
798
 * Author:
799
799
 *     Aly
800
800
 *
801
801
 * Comments: !!! Don't forget sync code with C !!!
802
802
 *
803
803
 */
804
 
         
 
804
 
805
805
function update_services_status_all(){
806
806
 
807
 
        clear_parents_from_trigger();   
 
807
        clear_parents_from_trigger();
808
808
 
809
809
        $result = DBselect('SELECT s.serviceid,s.algorithm,s.triggerid '.
810
810
                                                ' FROM services s '.
815
815
                DBexecute('UPDATE services SET status = '.$status.' WHERE serviceid='.$rows['serviceid']);
816
816
 
817
817
                add_service_alarm($rows['serviceid'],$status,time());
818
 
        }       
 
818
        }
819
819
 
820
820
        $result = DBselect('SELECT MAX(sl.servicedownid) as serviceid, sl.serviceupid '.
821
821
                                                ' FROM services_links sl '.
858
858
        }
859
859
 
860
860
        $result = DBexecute('INSERT INTO service_alarms (servicealarmid,serviceid,clock,value) VALUES ('.get_dbid('service_alarms','servicealarmid').','.$serviceid.','.$clock.','.$status.')');
861
 
        
 
861
 
862
862
        return $result;
863
863
}
864
864
?>