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

« back to all changes in this revision

Viewing changes to frontends/php/include/hosts.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:
62
62
                        error("incorrect parameters for 'db_save_group'");
63
63
                        return false;
64
64
                }
65
 
        
 
65
 
66
66
                if(is_null($groupid))
67
67
                        $result = DBselect("select * from groups where ".DBin_node('groupid')." AND name=".zbx_dbstr($name));
68
68
                else
69
69
                        $result = DBselect("select * from groups where ".DBin_node('groupid')." AND name=".zbx_dbstr($name).
70
70
                                " and groupid<>$groupid");
71
 
                
 
71
 
72
72
                if(DBfetch($result)){
73
73
                        error("Group '$name' already exists");
74
74
                        return false;
75
75
                }
76
 
                
 
76
 
77
77
                if(is_null($groupid)){
78
78
                        $groupid=get_dbid("groups","groupid");
79
79
                        if(!DBexecute("insert into groups (groupid,name) values (".$groupid.",".zbx_dbstr($name).")"))
95
95
                        $groupid = db_save_group($newgroup);
96
96
                        if(!$groupid)
97
97
                                return  $groupid;
98
 
                }               
 
98
                }
99
99
                return add_host_to_group($hostid, $groupid);
100
100
        }
101
101
 
119
119
                $groupid = db_save_group($name);
120
120
                if(!$groupid)
121
121
                        return  $groupid;
122
 
                
 
122
 
123
123
                update_host_groups_by_groupid($groupid,$hosts);
124
124
 
125
125
                return $groupid;
129
129
                $result = db_save_group($name,$groupid);
130
130
                if(!$result)
131
131
                        return  $result;
132
 
                
 
132
 
133
133
                update_host_groups_by_groupid($groupid,$hosts);
134
134
 
135
135
                return $result;
139
139
         * Function: check_circle_host_link
140
140
         *
141
141
         * Description:
142
 
         *     Check templates linage circeling
 
142
         *     Check for circular templates linkage
143
143
         *
144
144
         * Author:
145
145
         *     Eugene Grigorjev (eugene.grigorjev@zabbix.com)
155
155
                foreach($templates as $id => $name)
156
156
                        if(check_circle_host_link($hostid, get_templates_by_hostid($id)))
157
157
                                return true;
158
 
                        
 
158
 
159
159
                return false;
160
160
        }
161
161
 
216
216
                }
217
217
                else{
218
218
                        if(check_circle_host_link($hostid, $templates)){
219
 
                                error("Circle link can't be created");
 
219
                                error("Circular link can't be created");
220
220
                                return false;
221
221
                        }
222
222
                        $result = DBexecute('UPDATE hosts SET proxy_hostid='.$proxy_hostid.
236
236
 
237
237
                        update_host_status($hostid, $status);
238
238
                }
239
 
                
 
239
 
240
240
                foreach($templates as $id => $name){
241
241
                        $hosttemplateid = get_dbid('hosts_templates', 'hosttemplateid');
242
242
                        if(!($result = DBexecute('insert into hosts_templates values ('.$hosttemplateid.','.$hostid.','.$id.')')))
274
274
                add_group_to_host($hostid,$newgroup);
275
275
 
276
276
                sync_host_with_templates($hostid);
277
 
                
 
277
 
278
278
                return  $hostid;
279
279
        }
280
280
 
290
290
         * Comments:
291
291
         *
292
292
         *     NOTE: templates = array(id => name, id2 => name2, ...)
293
 
         */     
 
293
         */
294
294
        function update_host($hostid,$host,$port,$status,$useip,$dns,$ip,$proxy_hostid,$templates,$useipmi,$ipmi_ip,$ipmi_port,$ipmi_authtype,$ipmi_privilege,$ipmi_username,$ipmi_password,$newgroup,$groups)
295
295
        {
296
 
        
 
296
 
297
297
                $old_templates = get_templates_by_hostid($hostid);
298
298
                $unlinked_templates = array_diff($old_templates, $templates);
299
 
                
 
299
 
300
300
                foreach($unlinked_templates as $id => $name){
301
301
                        unlink_template($hostid, $id);
302
302
                }
303
 
                
 
303
 
304
304
                $old_host = get_host_by_hostid($hostid);
305
305
 
306
306
                $new_templates = array_diff($templates, $old_templates);
333
333
         */
334
334
        function unlink_template($hostid, $templateids, $unlink_mode = true){
335
335
                zbx_value2array($templateids);
336
 
                
 
336
 
337
337
                $result = delete_template_elements($hostid, $templateids, $unlink_mode);
338
338
                $result&= DBexecute('DELETE FROM hosts_templates WHERE hostid='.$hostid.' AND '.DBcondition('templateid',$templateids));
339
339
        return $result;
353
353
         */
354
354
        function delete_template_elements($hostid, $templateids = null, $unlink_mode = false){
355
355
                zbx_value2array($templateids);
356
 
                
 
356
 
357
357
                delete_template_graphs($hostid, $templateids, $unlink_mode);
358
358
                delete_template_triggers($hostid, $templateids, $unlink_mode);
359
359
                delete_template_items($hostid, $templateids, $unlink_mode);
360
360
                delete_template_applications($hostid, $templateids, $unlink_mode);
361
361
        return true;
362
 
        }       
 
362
        }
363
363
 
364
364
        /*
365
 
         * Function: copy_template_elements 
 
365
         * Function: copy_template_elements
366
366
         *
367
367
         * Description:
368
368
         *     Copy all elements from template to host
393
393
         *
394
394
         */
395
395
        function sync_host_with_templates($hostid, $templateid = null){
396
 
                delete_template_elements($hostid, $templateid);         
 
396
                delete_template_elements($hostid, $templateid);
397
397
                copy_template_elements($hostid, $templateid);
398
398
        }
399
399
 
401
401
                $sql="select groupid from hosts_groups where hostid=$hostid";
402
402
                $result=DBselect($sql);
403
403
                while($row=DBfetch($result)){
404
 
                
 
404
 
405
405
                        $sql="delete from hosts_groups where hostid=$hostid and groupid=".$row["groupid"];
406
406
                        DBexecute($sql);
407
 
                        
 
407
 
408
408
                        $sql="select count(*) as count from hosts_groups where groupid=".$row["groupid"];
409
409
                        $result2=DBselect($sql);
410
410
                        $row2=DBfetch($result2);
428
428
 */
429
429
        function delete_host($hostids, $unlink_mode = false){
430
430
                zbx_value2array($hostids);
431
 
                
432
 
                $ret = false;           
 
431
 
 
432
                $ret = false;
433
433
// unlink child hosts
434
434
                $db_childs = get_hosts_by_templateid($hostids);
435
435
                while($db_child = DBfetch($db_childs)){
448
448
 
449
449
// delete host from maps
450
450
                delete_sysmaps_elements_with_hostid($hostids);
451
 
                
 
451
 
452
452
// delete host from group
453
453
                DBexecute('DELETE FROM hosts_groups WHERE '.DBcondition('hostid',$hostids));
454
454
 
457
457
 
458
458
// delete host applications
459
459
                DBexecute('DELETE FROM applications WHERE '.DBcondition('hostid',$hostids));
460
 
                
 
460
 
461
461
// disable actions
462
462
                $actionids = array();
463
 
                
 
463
 
464
464
// conditions
465
465
                $sql = 'SELECT DISTINCT actionid '.
466
466
                                ' FROM conditions '.
474
474
                DBexecute('UPDATE actions '.
475
475
                                        ' SET status='.ACTION_STATUS_DISABLED.
476
476
                                        ' WHERE '.DBcondition('actionid',$actionids));
477
 
// operations           
 
477
// operations
478
478
                $sql = 'SELECT DISTINCT o.actionid '.
479
479
                                ' FROM operations o '.
480
480
                                ' WHERE o.operationtype IN ('.OPERATION_TYPE_GROUP_ADD.','.OPERATION_TYPE_GROUP_REMOVE.') '.
483
483
                while($db_action = DBfetch($db_actions)){
484
484
                        $actionids[$db_action['actionid']] = $db_action['actionid'];
485
485
                }
486
 
                
 
486
 
487
487
                if(!empty($actionids)){
488
488
                        DBexecute('UPDATE actions '.
489
489
                                        ' SET status='.ACTION_STATUS_DISABLED.
501
501
                DBexecute('DELETE FROM operations '.
502
502
                                        ' WHERE operationtype IN ('.OPERATION_TYPE_TEMPLATE_ADD.','.OPERATION_TYPE_TEMPLATE_REMOVE.') '.
503
503
                                                ' AND '.DBcondition('objectid',$hostids));
504
 
                                                
505
 
                                                
 
504
 
 
505
 
506
506
// delete host profile
507
507
                delete_host_profile($hostids);
508
508
                delete_host_profile_ext($hostids);
509
 
                
 
509
 
510
510
// delete web tests
511
511
                $del_httptests = array();
512
512
                $db_httptests = get_httptests_by_hostid($hostids);
523
523
 
524
524
        function delete_host_group($groupids){
525
525
                zbx_value2array($groupids);
526
 
                
 
526
 
527
527
                if(!delete_sysmaps_elements_with_groupid($groupids))
528
528
                        return false;
529
 
                
 
529
 
530
530
// disable actions
531
531
                $actionids = array();
532
 
                
 
532
 
533
533
// conditions
534
534
                $sql = 'SELECT DISTINCT c.actionid '.
535
535
                                ' FROM conditions c '.
540
540
                        $actionids[$db_action['actionid']] = $db_action['actionid'];
541
541
                }
542
542
 
543
 
// operations           
 
543
// operations
544
544
                $sql = 'SELECT DISTINCT o.actionid '.
545
545
                                ' FROM operations o '.
546
546
                                ' WHERE o.operationtype IN ('.OPERATION_TYPE_GROUP_ADD.','.OPERATION_TYPE_GROUP_REMOVE.') '.
549
549
                while($db_action = DBfetch($db_actions)){
550
550
                        $actionids[$db_action['actionid']] = $db_action['actionid'];
551
551
                }
552
 
                
 
552
 
553
553
                if(!empty($actionids)){
554
554
                        DBexecute('UPDATE actions '.
555
555
                                        ' SET status='.ACTION_STATUS_DISABLED.
556
556
                                        ' WHERE '.DBcondition('actionid',$actionids));
557
557
                }
558
 
                
 
558
 
559
559
 
560
560
// delete action conditions
561
561
                DBexecute('DELETE FROM conditions'.
566
566
                DBexecute('DELETE FROM operations '.
567
567
                                        ' WHERE operationtype IN ('.OPERATION_TYPE_GROUP_ADD.','.OPERATION_TYPE_GROUP_REMOVE.') '.
568
568
                                                ' AND '.DBcondition('objectid',$groupids));
569
 
                                                
 
569
 
570
570
                if(!DBexecute('DELETE FROM hosts_groups WHERE '.DBcondition('groupid',$groupids)))
571
571
                        return false;
572
572
 
582
582
                error("No host groups with groupid=[$groupid]");
583
583
                return  false;
584
584
        }
585
 
        
 
585
 
586
586
        function get_groupids_by_host($hostid){
587
587
                $groupids = array();
588
 
                
 
588
 
589
589
                $result=DBselect('SELECT DISTINCT hg.groupid '.
590
590
                                                ' FROM hosts_groups hg '.
591
591
                                                ' WHERE hg.hostid='.$hostid);
592
592
                while($row=DBfetch($result)){
593
593
                        $groupids[$row['groupid']] = $row['groupid'];
594
594
                }
595
 
                
 
595
 
596
596
        return $groupids;
597
597
        }
598
598
 
601
601
                        error("incorrect parameters for 'db_save_proxy'");
602
602
                        return false;
603
603
                }
604
 
        
 
604
 
605
605
                if(is_null($proxyid))
606
606
                        $result = DBselect('SELECT * FROM hosts WHERE status IN ('.HOST_STATUS_PROXY.')'.
607
607
                                        ' and '.DBin_node('hostid').' AND host='.zbx_dbstr($name));
609
609
                        $result = DBselect('SELECT * FROM hosts WHERE status IN ('.HOST_STATUS_PROXY.')'.
610
610
                                        ' and '.DBin_node('hostid').' AND host='.zbx_dbstr($name).
611
611
                                        ' and hostid<>'.$proxyid);
612
 
                
 
612
 
613
613
                if(DBfetch($result)){
614
614
                        error("Proxy '$name' already exists");
615
615
                        return false;
616
616
                }
617
 
                
 
617
 
618
618
                if(is_null($proxyid)){
619
619
                        $proxyid=get_dbid('hosts','hostid');
620
620
                        if(!DBexecute('INSERT INTO hosts (hostid,host,status)'.
622
622
                        {
623
623
                                return false;
624
624
                        }
625
 
                        
 
625
 
626
626
                        return $proxyid;
627
627
                }
628
628
                else
667
667
 
668
668
        function get_host_by_itemid($itemids){
669
669
                $res_array = is_array($itemids);
670
 
                zbx_value2array($itemids);      
671
 
                
 
670
                zbx_value2array($itemids);
 
671
 
672
672
                $result = false;
673
673
                $hosts = array();
674
 
                
 
674
 
675
675
                $sql = 'SELECT i.itemid, h.* FROM hosts h, items i WHERE i.hostid=h.hostid AND '.DBcondition('i.itemid',$itemids);
676
676
 
677
677
                $res=DBselect($sql);
684
684
                        $result = true;
685
685
                        $hosts[$row['itemid']] = $row;
686
686
                }
687
 
                
 
687
 
688
688
                if(!$res_array){
689
689
                        foreach($hosts as $itemid => $host){
690
690
                                $result = $host;
694
694
                        $result = $hosts;
695
695
                        unset($hosts);
696
696
                }
697
 
                
 
697
 
698
698
        return $result;
699
699
        }
700
700
 
701
701
        function get_host_by_hostid($hostid,$no_error_message=0){
702
 
        
 
702
 
703
703
                $sql='SELECT * FROM hosts WHERE hostid='.$hostid;
704
704
                $result=DBselect($sql);
705
705
                $row=DBfetch($result);
729
729
 
730
730
        function update_host_status($hostids,$status){
731
731
                zbx_value2array($hostids);
732
 
                
 
732
 
733
733
                $hosts = array();
734
734
                $result = DBselect('SELECT host, hostid, status FROM hosts WHERE '.DBcondition('hostid',$hostids));
735
735
                while($host=DBfetch($result)){
751
751
                        return 1;
752
752
                }
753
753
        }
754
 
        
 
754
 
755
755
/*
756
756
 * Function: get_templates_by_hostid
757
757
 *
771
771
                                        ' LEFT JOIN hosts h ON h.hostid=ht.templateid '.
772
772
                                ' WHERE ht.hostid='.$hostid.
773
773
                                ' ORDER BY h.host');
774
 
                                
 
774
 
775
775
                while($template_data = DBfetch($db_templates)){
776
776
                        $result[$template_data['hostid']] = $template_data['host'];
777
777
                }
778
 
                
 
778
 
 
779
        return $result;
 
780
        }
 
781
 
 
782
/*
 
783
 * Function: get_viewed_nodes
 
784
 *
 
785
 * Description:
 
786
 *     Retrive nodes for dropdown
 
787
 *
 
788
 * Author:
 
789
 *              Artem "Aly" Suharev
 
790
 *
 
791
 * Comments:
 
792
 *
 
793
 */
 
794
        function get_viewed_nodes(){
 
795
                global $USER_DETAILS;
 
796
                global $ZBX_NODES;
 
797
                global $ZBX_LOCALNODEID;
 
798
 
 
799
                $result = array('selected'=>0, 'nodes'=> array(), 'nodeids'=> array());
 
800
 
 
801
                $result['selected'] = get_current_nodeid(false);
 
802
 
 
803
                $available_nodes = get_accessible_nodes_by_user($USER_DETAILS,PERM_READ_ONLY,PERM_RES_DATA_ARRAY);
 
804
                $available_nodes = get_tree_by_parentid($ZBX_LOCALNODEID,$available_nodes,'masterid');
 
805
 
 
806
                foreach($available_nodes as $key => $node){
 
807
                        $result['nodes'][$node['nodeid']] = $node;
 
808
                        $result['nodeids'][$node['nodeid']] = $node['nodeid'];
 
809
                }
 
810
 
779
811
        return $result;
780
812
        }
781
813
 
789
821
 *              Artem "Aly" Suharev
790
822
 *
791
823
 * Comments:
792
 
 *      
 
824
 *
793
825
 */
794
826
function get_viewed_groups($perm, $options=array(), $nodeid=null, $sql=array()){
795
827
        global $USER_DETAILS;
796
828
        global $page;
797
 
        
 
829
 
798
830
        $def_sql = array(
799
831
                                'select' =>     array('g.groupid','g.name'),
800
832
                                'from' =>       array('groups g'),
801
833
                                'where' =>      array(),
802
834
                                'order' =>      array(),
803
835
                        );
804
 
                        
 
836
 
805
837
        $def_options = array(
806
838
                                'allow_all' =>                                  0,
807
839
                                'select_first_group'=>                  0,
821
853
                                'with_monitored_httptests'=>    0,
822
854
                                'with_graphs'=>                                 0,
823
855
                                'only_current_node' =>                  0,
824
 
                        );      
 
856
                        );
825
857
        $def_options = array_merge($def_options, $options);
826
 
        
 
858
 
827
859
        $dd_first_entry = ZBX_DROPDOWN_FIRST_ENTRY;
828
860
        if($dd_first_entry == ZBX_DROPDOWN_FIRST_ZBX162){
829
861
                $def_options['select_first_group_if_empty'] = 1;
835
867
        $result = array('original'=> -1, 'selected'=>0, 'groups'=> array(), 'groupids'=> array());
836
868
        $groups = &$result['groups'];
837
869
        $groupids = &$result['groupids'];
838
 
        
 
870
 
839
871
        $first_entry = ($dd_first_entry == ZBX_DROPDOWN_FIRST_NONE)?S_NOT_SELECTED_SMALL:S_ALL_SMALL;
840
872
        $groups['0'] = $first_entry;
841
873
        $groupids['0'] = '0';
842
 
        
 
874
 
843
875
        $_REQUEST['groupid'] = $result['original'] = get_request('groupid', -1);
844
876
        $_REQUEST['hostid'] = get_request('hostid', -1);
845
877
//-----
846
 
        
 
878
 
847
879
        $nodeid = is_null($nodeid)?get_current_nodeid(!$def_options['only_current_node']):$nodeid;
848
880
        $available_groups = get_accessible_groups_by_user($USER_DETAILS,$perm,PERM_RES_IDS_ARRAY,$nodeid,AVAILABLE_NOCACHE);
849
881
 
850
882
// nodes
851
883
        if(ZBX_DISTRIBUTED){
852
 
                $def_sql['select'][] = 'n.name';
 
884
                $def_sql['select'][] = 'n.name as node_name';
853
885
                $def_sql['from'][] = 'nodes n';
854
886
                $def_sql['where'][] = 'n.nodeid='.DBid2nodeid('g.groupid');
855
 
                $def_sql['order'][] = 'n.name';
 
887
                $def_sql['order'][] = 'node_name';
856
888
        }
857
 
        
 
889
 
858
890
// hosts
859
891
        if($def_options['monitored_hosts'])
860
892
                $def_sql['where'][] = 'h.status='.HOST_STATUS_MONITORED;
873
905
                $def_sql['where'][] = 'hg.groupid=g.groupid';
874
906
                $def_sql['where'][] = 'h.hostid=hg.hostid';
875
907
        }
876
 
        
 
908
 
877
909
// items
878
910
        if($def_options['with_items']){
879
911
                $def_sql['from'][] = 'hosts_groups hg';
880
912
 
881
913
                $def_sql['where'][] = 'hg.groupid=g.groupid';
882
914
                $def_sql['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE hg.hostid=i.hostid )';
883
 
        } 
 
915
        }
884
916
        else if($def_options['with_monitored_items']){
885
917
                $def_sql['from'][] = 'hosts_groups hg';
886
918
 
897
929
// triggers
898
930
        if($def_options['with_triggers']){
899
931
                $def_sql['from'][] = 'hosts_groups hg';
900
 
                
 
932
 
901
933
                $def_sql['where'][] = 'hg.groupid=g.groupid';
902
934
                $def_sql['where'][] = 'EXISTS( SELECT t.triggerid '.
903
935
                                                                        ' FROM items i, functions f, triggers t'.
904
936
                                                                        ' WHERE i.hostid=hg.hostid '.
905
937
                                                                                ' AND f.itemid=i.itemid '.
906
938
                                                                                ' AND t.triggerid=f.triggerid)';
907
 
        }       
 
939
        }
908
940
        else if($def_options['with_monitored_triggers']){
909
941
                $def_sql['from'][] = 'hosts_groups hg';
910
 
                
 
942
 
911
943
                $def_sql['where'][] = 'hg.groupid=g.groupid';
912
944
                $def_sql['where'][] = 'EXISTS( SELECT t.triggerid '.
913
945
                                                                        ' FROM items i, functions f, triggers t'.
917
949
                                                                                ' AND f.triggerid=t.triggerid '.
918
950
                                                                                ' AND t.status='.TRIGGER_STATUS_ENABLED.')';
919
951
        }
920
 
        
921
 
// htptests     
 
952
 
 
953
// htptests
922
954
        if($def_options['with_httptests']){
923
955
                $def_sql['from'][] = 'hosts_groups hg';
924
 
                
 
956
 
925
957
                $def_sql['where'][] = 'hg.groupid=g.groupid';
926
958
                $def_sql['where'][] = 'EXISTS( SELECT a.applicationid '.
927
959
                                                                ' FROM applications a, httptest ht '.
930
962
        }
931
963
        else if($def_options['with_monitored_httptests']){
932
964
                $def_sql['from'][] = 'hosts_groups hg';
933
 
                
 
965
 
934
966
                $def_sql['where'][] = 'hg.groupid=g.groupid';
935
967
                $def_sql['where'][] = 'EXISTS( SELECT a.applicationid '.
936
968
                                                                ' FROM applications a, httptest ht '.
938
970
                                                                        ' AND ht.applicationid=a.applicationid '.
939
971
                                                                        ' AND ht.status='.HTTPTEST_STATUS_ACTIVE.')';
940
972
        }
941
 
        
 
973
 
942
974
// graphs
943
975
        if($def_options['with_graphs']){
944
976
                $def_sql['from'][] = 'hosts_groups hg';
945
 
                
 
977
 
946
978
                $def_sql['where'][] = 'hg.groupid=g.groupid';
947
979
                $def_sql['where'][] = 'EXISTS( SELECT DISTINCT i.itemid '.
948
980
                                                                        ' FROM items i, graphs_items gi '.
949
981
                                                                        ' WHERE i.hostid=hg.hostid '.
950
982
                                                                                ' AND i.itemid=gi.itemid)';
951
983
        }
952
 
        
 
984
 
953
985
//-----
954
986
        $def_sql['order'][] = 'g.name';
955
 
        
 
987
 
956
988
        foreach($sql as $key => $value){
957
989
                zbx_value2array($value);
958
990
 
959
991
                if(isset($def_sql[$key])) $def_sql[$key] = array_merge($def_sql[$key], $value);
960
992
                else $def_sql[$key] = $value;
961
993
        }
962
 
        
 
994
 
963
995
        $def_sql['select'] = array_unique($def_sql['select']);
964
996
        $def_sql['from'] = array_unique($def_sql['from']);
965
997
        $def_sql['where'] = array_unique($def_sql['where']);
984
1016
        while($group = DBfetch($res)){
985
1017
                $groups[$group['groupid']] = $group['name'];
986
1018
                $groupids[$group['groupid']] = $group['groupid'];
987
 
                
 
1019
 
988
1020
                if(bccomp($_REQUEST['groupid'],$group['groupid']) == 0) $result['selected'] = $group['groupid'];
989
1021
        }
990
1022
 
996
1028
        else if($def_options['do_not_select_if_empty'] && ($_REQUEST['groupid'] == -1)){
997
1029
                $result['selected'] = $_REQUEST['groupid'] = 0;
998
1030
        }
999
 
        else if(($def_options['select_first_group']) || 
 
1031
        else if(($def_options['select_first_group']) ||
1000
1032
                        ($def_options['select_first_group_if_empty'] && ($_REQUEST['groupid'] == -1) && is_null($profile_groupid)))
1001
1033
        {
1002
1034
                $first_groupid = next($groupids);
1003
1035
                reset($groupids);
1004
 
                
 
1036
 
1005
1037
                if($first_groupid !== FALSE)
1006
1038
                        $_REQUEST['groupid'] = $result['selected'] = $first_groupid;
1007
 
                else 
 
1039
                else
1008
1040
                        $_REQUEST['groupid'] = $result['selected'] = 0;
1009
1041
        }
1010
1042
        else{
1034
1066
 *              Artem "Aly" Suharev
1035
1067
 *
1036
1068
 * Comments:
1037
 
 *      
 
1069
 *
1038
1070
 */
1039
1071
function get_viewed_hosts($perm, $groupid=0, $options=array(), $nodeid=null, $sql=array('monitored_hosts'=>1)){
1040
1072
        global $USER_DETAILS;
1068
1100
                                'with_graphs'=>                                 0,
1069
1101
                                'only_current_node' =>                  0,
1070
1102
                        );
1071
 
                        
 
1103
 
1072
1104
        $def_options = array_merge($def_options, $options);
1073
1105
 
1074
1106
        $dd_first_entry = ZBX_DROPDOWN_FIRST_ENTRY;
1082
1114
        $result = array('original'=> -1, 'selected'=>0, 'hosts'=> array(), 'hostids'=> array());
1083
1115
        $hosts = &$result['hosts'];
1084
1116
        $hostids = &$result['hostids'];
1085
 
        
 
1117
 
1086
1118
        $first_entry = ($dd_first_entry == ZBX_DROPDOWN_FIRST_NONE)?S_NOT_SELECTED_SMALL:S_ALL_SMALL;
1087
1119
        $hosts['0'] = $first_entry;
1088
1120
        $hostids['0'] = '0';
1089
 
        
 
1121
 
1090
1122
        if(!is_array($groupid) && ($groupid == 0)){
1091
1123
                if($dd_first_entry == ZBX_DROPDOWN_FIRST_NONE){
1092
1124
                        return $result;
1099
1131
                $def_sql['where'][] = DBcondition('hg.groupid',$groupid);
1100
1132
                $def_sql['where'][] = 'hg.hostid=h.hostid';
1101
1133
        }
1102
 
        
 
1134
 
1103
1135
        $_REQUEST['hostid'] = $result['original'] = get_request('hostid', -1);
1104
1136
//-----
1105
 
        
 
1137
 
1106
1138
        $nodeid = is_null($nodeid)?get_current_nodeid(!$def_options['only_current_node']):$nodeid;
1107
1139
        $available_hosts = get_accessible_hosts_by_user($USER_DETAILS,$perm,PERM_RES_IDS_ARRAY,$nodeid,AVAILABLE_NOCACHE);
1108
1140
 
1128
1160
// items
1129
1161
        if($def_options['with_items']){
1130
1162
                $def_sql['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid )';
1131
 
        }               
 
1163
        }
1132
1164
        else if($def_options['with_monitored_items']){
1133
1165
                $def_sql['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid AND i.status='.ITEM_STATUS_ACTIVE.')';
1134
1166
        }
1135
1167
        else if($def_options['with_historical_items']){
1136
1168
                $def_sql['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid AND (i.status='.ITEM_STATUS_ACTIVE.' OR i.status='.ITEM_STATUS_NOTSUPPORTED.') AND i.lastvalue IS NOT NULL)';
1137
1169
        }
1138
 
        
1139
 
                
 
1170
 
 
1171
 
1140
1172
// triggers
1141
1173
        if($def_options['with_triggers']){
1142
1174
                $def_sql['where'][] = 'EXISTS( SELECT i.itemid '.
1144
1176
                                                                        ' WHERE i.hostid=h.hostid '.
1145
1177
                                                                                ' AND i.itemid=f.itemid '.
1146
1178
                                                                                ' AND f.triggerid=t.triggerid)';
1147
 
        }       
 
1179
        }
1148
1180
        else if($def_options['with_monitored_triggers']){
1149
1181
                $def_sql['where'][] = 'EXISTS( SELECT i.itemid '.
1150
1182
                                                                        ' FROM items i, functions f, triggers t'.
1169
1201
                                                                        ' AND ht.applicationid=a.applicationid '.
1170
1202
                                                                        ' AND ht.status='.HTTPTEST_STATUS_ACTIVE.')';
1171
1203
        }
1172
 
        
 
1204
 
1173
1205
// graphs
1174
1206
        if($def_options['with_graphs']){
1175
1207
                $def_sql['where'][] = 'EXISTS( SELECT DISTINCT i.itemid '.
1179
1211
        }
1180
1212
//------
1181
1213
        $def_sql['order'][] = 'h.host';
1182
 
        
 
1214
 
1183
1215
        foreach($sql as $key => $value){
1184
1216
                zbx_value2array($value);
1185
1217
 
1186
1218
                if(isset($def_sql[$key])) $def_sql[$key] = array_merge($def_sql[$key], $value);
1187
1219
                else $def_sql[$key] = $value;
1188
1220
        }
1189
 
        
 
1221
 
1190
1222
        $def_sql['select'] = array_unique($def_sql['select']);
1191
1223
        $def_sql['from'] = array_unique($def_sql['from']);
1192
1224
        $def_sql['where'] = array_unique($def_sql['where']);
1200
1232
        if(!empty($def_sql['from'])) $sql_from.= implode(',',$def_sql['from']);
1201
1233
        if(!empty($def_sql['where'])) $sql_where.= ' AND '.implode(' AND ',$def_sql['where']);
1202
1234
        if(!empty($def_sql['order'])) $sql_order.= implode(',',$def_sql['order']);
1203
 
        
 
1235
 
1204
1236
        $sql = 'SELECT DISTINCT '.$sql_select.
1205
1237
                        ' FROM '.$sql_from.
1206
1238
                        ' WHERE '.DBcondition('h.hostid',$available_hosts).
1207
1239
                                $sql_where.
1208
 
                        ' ORDER BY '.$sql_order;        
 
1240
                        ' ORDER BY '.$sql_order;
1209
1241
        $res = DBselect($sql);
1210
1242
        while($host = DBfetch($res)){
1211
1243
                $hosts[$host['hostid']] = $host['host'];
1212
1244
                $hostids[$host['hostid']] = $host['hostid'];
1213
 
                
 
1245
 
1214
1246
                if(bccomp($_REQUEST['hostid'],$host['hostid']) == 0) $result['selected'] = $host['hostid'];
1215
1247
        }
1216
1248
 
1217
1249
        $profile_hostid = get_profile('web.'.$page['menu'].'.hostid');
1218
1250
 
1219
 
//----- 
 
1251
//-----
1220
1252
        if($def_options['do_not_select']){
1221
 
                $_REQUEST['hostid'] = $result['selected'] = 0;  
 
1253
                $_REQUEST['hostid'] = $result['selected'] = 0;
1222
1254
        }
1223
1255
        else if($def_options['do_not_select_if_empty'] && ($_REQUEST['hostid'] == -1)){
1224
1256
                $_REQUEST['hostid'] = $result['selected'] = 0;
1225
1257
        }
1226
 
        else if(($def_options['select_first_host']) || 
1227
 
                        ($def_options['select_first_host_if_empty'] && ($_REQUEST['hostid'] == -1) && is_null($profile_hostid)) || 
 
1258
        else if(($def_options['select_first_host']) ||
 
1259
                        ($def_options['select_first_host_if_empty'] && ($_REQUEST['hostid'] == -1) && is_null($profile_hostid)) ||
1228
1260
                        ($def_options['select_host_on_group_switch'] && ($_REQUEST['hostid'] != -1) && (bccomp($_REQUEST['hostid'],$result['selected']) != 0)))
1229
1261
        {
1230
1262
                $first_hostid = next($hostids);
1232
1264
 
1233
1265
                if($first_hostid !== FALSE)
1234
1266
                        $_REQUEST['hostid'] = $result['selected'] = $first_hostid;
1235
 
                else 
 
1267
                else
1236
1268
                        $_REQUEST['hostid'] = $result['selected'] = 0;
1237
1269
        }
1238
1270
        else{
1251
1283
        }
1252
1284
return $result;
1253
1285
}
1254
 
        
 
1286
 
1255
1287
/*
1256
1288
 * Function: validate_group_with_host
1257
1289
 *
1267
1299
 */
1268
1300
        function validate_group_with_host(&$PAGE_GROUPS, &$PAGE_HOSTS, $reset_host=true){
1269
1301
                global $page;
1270
 
                
 
1302
 
1271
1303
                $dd_first_entry = ZBX_DROPDOWN_FIRST_ENTRY;
1272
1304
                if($dd_first_entry == ZBX_DROPDOWN_FIRST_ZBX162){
1273
1305
                        $dd_first_entry = ZBX_DROPDOWN_FIRST_ALL;
1278
1310
 
1279
1311
                $_REQUEST['groupid']    = get_request('groupid', get_profile($group_var, -1));
1280
1312
                $_REQUEST['hostid']     = get_request('hostid', get_profile($host_var, -1));
1281
 
                
 
1313
 
1282
1314
                if($_REQUEST['groupid'] > 0){
1283
1315
                        if($_REQUEST['hostid'] > 0){
1284
1316
                                $sql = 'SELECT groupid FROM hosts_groups WHERE hostid='.$_REQUEST['hostid'].' AND groupid='.$_REQUEST['groupid'];
1292
1324
                }
1293
1325
                else{
1294
1326
                        $_REQUEST['groupid'] = 0;
1295
 
                        
 
1327
 
1296
1328
                        if($reset_host && ($dd_first_entry == ZBX_DROPDOWN_FIRST_NONE)){
1297
1329
                                $_REQUEST['hostid'] = 0;
1298
1330
                        }
1299
1331
                }
1300
 
                
 
1332
 
1301
1333
                $PAGE_GROUPS['selected'] = $_REQUEST['groupid'];
1302
1334
                $PAGE_HOSTS['selected'] = $_REQUEST['hostid'];
1303
 
                
 
1335
 
1304
1336
                if(($PAGE_HOSTS['selected'] == 0) && ($dd_first_entry == ZBX_DROPDOWN_FIRST_NONE) && $reset_host){
1305
1337
                        $PAGE_HOSTS['hostids'] = array(0);
1306
1338
                }
1307
 
                
 
1339
 
1308
1340
                if($PAGE_GROUPS['original'] > -1)
1309
1341
                        update_profile('web.'.$page['menu'].'.groupid', $_REQUEST['groupid'], PROFILE_TYPE_ID);
1310
 
                        
 
1342
 
1311
1343
                if($PAGE_HOSTS['original'] > -1)
1312
1344
                        update_profile('web.'.$page['menu'].'.hostid', $_REQUEST['hostid'], PROFILE_TYPE_ID);
1313
1345
 
1323
1355
 *
1324
1356
 * Author:
1325
1357
 *     Artem "Aly" Suharev
1326
 
 * 
 
1358
 *
1327
1359
 * Comments:
1328
 
 *      
 
1360
 *
1329
1361
 */
1330
1362
        function validate_group(&$PAGE_GROUPS, &$PAGE_HOSTS, $reset_host=true){
1331
1363
                global $page;
1332
1364
                $group_var = 'web.latest.groupid';
1333
1365
                $host_var = 'web.latest.hostid';
1334
 
                
 
1366
 
1335
1367
                $_REQUEST['groupid']    = get_request('groupid', get_profile($group_var, -1));
1336
 
                
 
1368
 
1337
1369
                if($_REQUEST['groupid'] < 0){
1338
1370
                        $PAGE_HOSTS['selected'] = $_REQUEST['groupid'] = 0;
1339
1371
                        $PAGE_HOSTS['selected'] = $_REQUEST['hostid'] = 0;
1340
1372
                }
1341
 
                
 
1373
 
1342
1374
                if(!isset($_REQUEST['hostid']) || $reset_host){
1343
1375
                        $PAGE_HOSTS['selected'] = $_REQUEST['hostid'] = 0;
1344
1376
                }
1345
 
                
 
1377
 
1346
1378
                $PAGE_GROUPS['selected'] = $_REQUEST['groupid'];
1347
1379
 
1348
1380
                if($PAGE_GROUPS['original'] > -1)
1349
1381
                        update_profile('web.'.$page['menu'].'.groupid', $_REQUEST['groupid'], PROFILE_TYPE_ID);
1350
 
                        
 
1382
 
1351
1383
                if($PAGE_HOSTS['original'] > -1)
1352
1384
                        update_profile('web.'.$page['menu'].'.hostid', $_REQUEST['hostid'], PROFILE_TYPE_ID);
1353
1385
 
1374
1406
                        error("Incorrect parameters for 'db_save_application'");
1375
1407
                        return false;
1376
1408
                }
1377
 
        
 
1409
 
1378
1410
                if(is_null($applicationid))
1379
1411
                        $result = DBselect('SELECT * FROM applications WHERE name='.zbx_dbstr($name).' AND hostid='.$hostid);
1380
1412
                else
1389
1421
                        error('Application "'.$name.'" already exists');
1390
1422
                        return false;
1391
1423
                }
1392
 
                
 
1424
 
1393
1425
                if($db_app && $applicationid!=null){ // delete old application with same name
1394
1426
                        delete_application($db_app["applicationid"]);
1395
1427
                }
1399
1431
                }
1400
1432
 
1401
1433
                $host = get_host_by_hostid($hostid);
1402
 
                
 
1434
 
1403
1435
                if(is_null($applicationid)){
1404
1436
                        $applicationid_new = get_dbid('applications','applicationid');
1405
 
                        
 
1437
 
1406
1438
                        $sql = 'INSERT INTO applications (applicationid,name,hostid,templateid) '.
1407
1439
                                        " VALUES ($applicationid_new,".zbx_dbstr($name).",$hostid,$templateid)";
1408
1440
                        if($result = DBexecute($sql)){
1473
1505
        function update_application($applicationid,$name,$hostid,$templateid=0){
1474
1506
                return db_save_application($name,$hostid,$applicationid,$templateid);
1475
1507
        }
1476
 
        
 
1508
 
1477
1509
        /*
1478
1510
         * Function: delete_application
1479
1511
         *
1503
1535
                        info("Application '".$host["host"].":".$app["name"]."' used by scenario '".$info['name']."'");
1504
1536
                        return false;
1505
1537
                }
1506
 
 
 
1538
 
1507
1539
                if($info = DBfetch(DBselect('select i.itemid,i.key_,i.description from items_applications ia, items i '.
1508
1540
                        ' where i.type='.ITEM_TYPE_HTTPTEST.' and i.itemid=ia.itemid and ia.applicationid='.$applicationid)))
1509
1541
                {
1532
1564
                if($no_error_message == 0)
1533
1565
                        error("No application with id=[$applicationid]");
1534
1566
                return  false;
1535
 
                
 
1567
 
1536
1568
        }
1537
1569
 
1538
1570
        function get_applications_by_templateid($applicationid){
1583
1615
         */
1584
1616
        function delete_template_applications($hostid, $templateids = null, $unlink_mode = false){
1585
1617
                zbx_value2array($templateids);
1586
 
                
 
1618
 
1587
1619
                $db_apps = get_applications_by_hostid($hostid);
1588
1620
                while($db_app = DBfetch($db_apps)){
1589
1621
                        if($db_app["templateid"] == 0)
1599
1631
                                        }
1600
1632
                                }
1601
1633
                                if(isset($skip)) continue;
1602
 
                                
 
1634
 
1603
1635
                        }
1604
 
                        
 
1636
 
1605
1637
                        if($unlink_mode){
1606
1638
                                if(DBexecute("update applications set templateid=0 where applicationid=".$db_app["applicationid"])){
1607
1639
                                        info("Application '".$db_app["name"]."' unlinked");
1631
1663
                if(null == $templateid){
1632
1664
                        $templateid = array_keys(get_templates_by_hostid($hostid));
1633
1665
                }
1634
 
                
 
1666
 
1635
1667
                if(is_array($templateid)){
1636
1668
                        foreach($templateid as $id)
1637
1669
                                copy_template_applications($hostid, $id, $copy_mode); // attention recursion
1657
1689
         * Author:
1658
1690
         *     Eugene Grigorjev (eugene.grigorjev@zabbix.com)
1659
1691
         *
1660
 
         * Comments: 
 
1692
         * Comments:
1661
1693
         *           $templateid_list can be numeric or numeric array
1662
1694
         *
1663
1695
         */
1665
1697
                if(is_numeric($templateid_list))return true;
1666
1698
                if(!is_array($templateid_list)) return false;
1667
1699
                if(count($templateid_list)<2)   return true;
1668
 
                
 
1700
 
1669
1701
                $result = true;
1670
 
                
 
1702
 
1671
1703
                $sql = 'SELECT key_,type,count(*) as cnt '.
1672
1704
                        ' FROM items '.
1673
1705
                        ' WHERE '.DBcondition('hostid',$templateid_list).
1697
1729
 
1698
1730
        return $result;
1699
1731
        }
1700
 
                                
 
1732
 
1701
1733
        function host_status2str($status){
1702
1734
                switch($status){
1703
1735
                        case HOST_STATUS_MONITORED:     $status = S_MONITORED;          break;
1709
1741
                }
1710
1742
                return $status;
1711
1743
        }
1712
 
        
 
1744
 
1713
1745
        function host_status2style($status){
1714
1746
                switch($status){
1715
1747
                        case HOST_STATUS_MONITORED:     $status = 'off';        break;
1719
1751
                }
1720
1752
                return $status;
1721
1753
        }
1722
 
        
 
1754
 
1723
1755
// Add Host Profile
1724
1756
 
1725
1757
        function add_host_profile($hostid,$devicetype,$name,$os,$serialno,$tag,$macaddress,$hardware,$software,$contact,$location,$notes){
1726
 
                
 
1758
 
1727
1759
                $result=DBselect('SELECT * FROM hosts_profiles WHERE hostid='.$hostid);
1728
1760
                if(DBfetch($result)){
1729
1761
                        error('Host profile already exists');
1736
1768
                        zbx_dbstr($os).','.zbx_dbstr($serialno).','.zbx_dbstr($tag).','.zbx_dbstr($macaddress).
1737
1769
                        ','.zbx_dbstr($hardware).','.zbx_dbstr($software).','.zbx_dbstr($contact).','.
1738
1770
                        zbx_dbstr($location).','.zbx_dbstr($notes).')');
1739
 
                
 
1771
 
1740
1772
        return  $result;
1741
1773
        }
1742
1774
 
1743
1775
/*
1744
1776
 * Function: add_host_profile_ext
1745
 
 *         
 
1777
 *
1746
1778
 * Description:
1747
1779
 *  Add alternate host profile information.
1748
1780
 *
1754
1786
 *
1755
1787
 */
1756
1788
        function add_host_profile_ext($hostid,$ext_host_profiles=array()){
1757
 
        
 
1789
 
1758
1790
                $ext_profiles_fields = array('device_alias','device_type','device_chassis','device_os','device_os_short',
1759
1791
                        'device_hw_arch','device_serial','device_model','device_tag','device_vendor','device_contract',
1760
1792
                        'device_who','device_status','device_app_01','device_app_02','device_app_03','device_app_04',
1770
1802
                        error('Host profile already exists');
1771
1803
                        return false;
1772
1804
                }
1773
 
                
 
1805
 
1774
1806
                $sql = 'INSERT INTO hosts_profiles_ext (hostid,';
1775
1807
                $values = ' VALUES ('.$hostid.',';
1776
1808
 
1780
1812
                                $values.=zbx_dbstr($value).',';
1781
1813
                        }
1782
1814
                }
1783
 
                
 
1815
 
1784
1816
                $sql = rtrim($sql,',').')';
1785
1817
                $values = rtrim($values,',').')';
1786
 
                
 
1818
 
1787
1819
                $result=DBexecute($sql.$values);
1788
1820
        return  $result;
1789
1821
        }
1796
1828
 
1797
1829
        return $result;
1798
1830
        }
1799
 
        
 
1831
 
1800
1832
/*
1801
1833
 * Function: delete_host_profile_ext
1802
1834
 *
1806
1838
 * Author:
1807
1839
 *     John R Pritchard (john.r.pritchard@gmail.com)
1808
1840
 *
1809
 
 * Comments: 
 
1841
 * Comments:
1810
1842
 *     Extend original "delete_host_profile" function for new hosts_profiles_ext data.
1811
1843
 *
1812
1844
 */
1819
1851
 
1820
1852
        function set_hosts_jsmenu_array($hostids = array()){
1821
1853
                $menu_all = array();
1822
 
                                        
 
1854
 
1823
1855
                $db_groups = DBselect('SELECT DISTINCT g.groupid, g.name '.
1824
1856
                                                ' FROM groups g '.
1825
1857
                                                ' WHERE '.DBin_node('g.groupid').
1826
1858
                                                ' ORDER BY g.name,g.groupid');
1827
 
                
 
1859
 
1828
1860
                while($group=DBfetch($db_groups)){
1829
1861
                        $group['name'] = htmlspecialchars($group['name']);
1830
1862
                        $menu_all[] = $group;
1831
1863
                }
1832
1864
                insert_js('var menu_hstgrp_all='.zbx_jsvalue($menu_all).";\n");
1833
1865
        }
1834
 
        
1835
 
        
 
1866
 
 
1867
 
1836
1868
        function host_js_menu($hostid,$link_text = S_SELECT){
1837
1869
                $hst_grp_all_in = array();
1838
 
                
 
1870
 
1839
1871
                $db_groups = DBselect('SELECT DISTINCT g.groupid, g.name '.
1840
1872
                                ' FROM groups g, hosts_groups hg '.
1841
1873
                                ' WHERE g.groupid=hg.groupid '.
1844
1876
 
1845
1877
                while($group = DBfetch($db_groups)){
1846
1878
                        $group['name'] = htmlspecialchars($group['name']);
1847
 
                        $hst_grp_all_in[] = $group;     
 
1879
                        $hst_grp_all_in[] = $group;
1848
1880
                }
1849
 
                                
 
1881
 
1850
1882
                $action = new CSpan($link_text);
1851
1883
                $script = new CScript('javascript: create_host_menu(event,'.$hostid.','.zbx_jsvalue($hst_grp_all_in).');');
1852
 
                                                         
 
1884
 
1853
1885
                $action->AddAction('onclick',$script);
1854
1886
                $action->AddOption('onmouseover','javascript: this.style.cursor = "pointer";');
1855
 
                
 
1887
 
1856
1888
        return $action;
1857
1889
        }
1858
1890