forked from olindata/tribily-zabbix-drupal-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzabbix_functions.php
executable file
·1361 lines (1072 loc) · 46.3 KB
/
zabbix_functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// $Id$
include_once('lib/ZabbixAPI.class.php');
define('HOST_NAME_MAXLENGTH', 64);
define('MEDIA_NAME_MAXLENGTH', 100);
define('HOST_SERVER_MAXLENGTH', 64);
define('DRUPAL_MSG_TYPE_ERR', 'error');
define('DRUPAL_MSG_TYPE_STATUS', 'status');
define('PAGER_COUNT', 100);
define('TABLE_ID_USER_MAPPING', 100);
define('TABLE_ID_HOSTS_MAPPING', 101);
define('TABLE_ID_EMAILS_MAPPING', 102);
//the permissions defined/used by this module
define('PERM_ADMIN', 'administer site configuration');
define('PERM_HOSTS_SELECT', 'access hosts');
define('PERM_HOSTS_INSERT', 'add hosts');
define('PERM_HOSTS_DELETE', 'delete hosts');
define('PERM_HOSTS_UPDATE', 'update hosts');
define('PERM_JABBER_SELECT', 'access jabbers');
define('PERM_JABBER_INSERT', 'add jabbers');
define('PERM_JABBER_DELETE', 'delete jabbers');
define('PERM_JABBER_UPDATE', 'update jabbers');
define('PERM_MOBILES_SELECT', 'access mobiles');
define('PERM_MOBILES_INSERT', 'add mobiles');
define('PERM_MOBILES_DELETE', 'delete mobiles');
define('PERM_MOBILES_UPDATE', 'update mobiles');
define('PERM_EMAIL_SELECT', 'access emails');
define('PERM_EMAIL_INSERT', 'add emails');
define('PERM_EMAIL_DELETE', 'delete emails');
define('PERM_EMAIL_UPDATE', 'update emails');
define('PERM_DASHBOARD', 'access dashboard');
// from zabbix/include/defines.inc.php
define('PERM_READ_WRITE', 3);
define('PERM_READ_ONLY', 2);
define('PERM_READ_LIST', 1);
define('PERM_DENY', 0);
define('EVENT_SOURCE_TRIGGERS', 0);
define('EVENT_SOURCE_DISCOVERY', 1);
define('EVENT_SOURCE_AUTO_REGISTRATION', 2);
define('ACTION_STATUS_ENABLED', 0);
define('ACTION_STATUS_DISABLED', 1);
define('ACTION_DEFAULT_MSG', '{TRIGGER.NAME}: {STATUS}');
define('ACTION_EVAL_TYPE_AND_OR', 0);
define('ACTION_EVAL_TYPE_AND', 1);
define('ACTION_EVAL_TYPE_OR', 2);
define('CONDITION_TYPE_HOST_GROUP', 0);
define('CONDITION_TYPE_HOST', 1);
define('CONDITION_TYPE_TRIGGER', 2);
define('CONDITION_TYPE_TRIGGER_NAME', 3);
define('CONDITION_TYPE_TRIGGER_SEVERITY', 4);
define('CONDITION_TYPE_TRIGGER_VALUE', 5);
define('CONDITION_TYPE_TIME_PERIOD', 6);
define('CONDITION_TYPE_DHOST_IP', 7);
define('CONDITION_TYPE_DSERVICE_TYPE', 8);
define('CONDITION_TYPE_DSERVICE_PORT', 9);
define('CONDITION_TYPE_DSTATUS', 10);
define('CONDITION_TYPE_DUPTIME', 11);
define('CONDITION_TYPE_DVALUE', 12);
define('CONDITION_TYPE_HOST_TEMPLATE', 13);
define('CONDITION_TYPE_EVENT_ACKNOWLEDGED', 14);
define('CONDITION_TYPE_APPLICATION', 15);
define('CONDITION_TYPE_MAINTENANCE', 16);
define('CONDITION_TYPE_NODE', 17);
define('CONDITION_TYPE_DRULE', 18);
define('CONDITION_TYPE_DCHECK', 19);
define('CONDITION_TYPE_PROXY', 20);
define('CONDITION_TYPE_DOBJECT', 21);
define('CONDITION_TYPE_HOST_NAME', 22);
define('TRIGGER_SEVERITY_NOT_CLASSIFIED', 0);
define('TRIGGER_SEVERITY_INFORMATION', 1);
define('TRIGGER_SEVERITY_WARNING', 2);
define('TRIGGER_SEVERITY_AVERAGE', 3);
define('TRIGGER_SEVERITY_HIGH', 4);
define('TRIGGER_SEVERITY_DISASTER', 5);
define('CONDITION_OPERATOR_EQUAL', 0);
define('CONDITION_OPERATOR_NOT_EQUAL', 1);
define('CONDITION_OPERATOR_LIKE', 2);
define('CONDITION_OPERATOR_NOT_LIKE', 3);
define('CONDITION_OPERATOR_IN', 4);
define('CONDITION_OPERATOR_MORE_EQUAL', 5);
define('CONDITION_OPERATOR_LESS_EQUAL', 6);
define('CONDITION_OPERATOR_NOT_IN', 7);
define('OPERATION_OBJECT_USER', 0);
define('OPERATION_OBJECT_GROUP', 1);
define('EVENT_ACK_DISABLED', '0');
define('EVENT_ACK_ENABLED', '1');
define('OPERATION_TYPE_MESSAGE', 0);
define('OPERATION_TYPE_COMMAND', 1);
define('OPERATION_TYPE_HOST_ADD', 2);
define('OPERATION_TYPE_HOST_REMOVE', 3);
define('OPERATION_TYPE_GROUP_ADD', 4);
define('OPERATION_TYPE_GROUP_REMOVE', 5);
define('OPERATION_TYPE_TEMPLATE_ADD', 6);
define('OPERATION_TYPE_TEMPLATE_REMOVE', 7);
define('OPERATION_TYPE_HOST_ENABLE', 8);
define('OPERATION_TYPE_HOST_DISABLE', 9);
//refers to database field media_type.type
// NOTE: this is equal for every zabbix install
define('MEDIA_TYPE_EMAIL', 0);
define('MEDIA_TYPE_EXEC', 1);
define('MEDIA_TYPE_SMS', 2);
define('MEDIA_TYPE_JABBER', 3);
//refers to database field media_type.mediatypeid
// NOTE: this is/can be different for every zabbix install!!
define('ALERT_TYPE_EMAIL', 1);
define('ALERT_TYPE_JABBER', 2);
define('ALERT_TYPE_SMS', 3);
define('TRIGGER_VALUE_FALSE', 0);
define('TRIGGER_VALUE_TRUE', 1);
define('TRIGGER_VALUE_UNKNOWN', 2);
define('API_OUTPUT_SHORTEN', 'shorten');
define('API_OUTPUT_REFER', 'refer');
define('API_OUTPUT_EXTEND', 'extend');
define('API_OUTPUT_COUNT', 'count');
define('API_OUTPUT_CUSTOM', 'custom');
define('STR_CUST_HOSTGROUP', 'Customer Hostgroup ');
define('STR_CUST_USERGROUP', 'Customer Usergroup ');
define('STR_DEFAULT_ACTION_SUBJECT', '{STATUS}: {TRIGGER.NAME}');
define('STR_DEFAULT_ACTION_TEXT', 'Alert: {TRIGGER.NAME}\nStatus: {STATUS}\nSeverity: {TRIGGER.SEVERITY}\nHost: {IPADDRESS}\nHost dns - {HOST.DNS1}\nURL: {TRIGGER.URL}\nLast Value: {ITEM.LASTVALUE}\nEvent age - {EVENT.AGE}\nEvent start date and time - {EVENT.DATE} , {EVENT.TIME}\nAcknowledgement status - {EVENT.ACK.STATUS}\nAcknowledgement history - {EVENT.ACK.HISTORY}');
define('STR_DEFAULT_ACTION_SUBJECT_MOBILE', '{STATUS}');
define('STR_DEFAULT_ACTION_TEXT_MOBILE', '{TRIGGER.NAME}');
define('STR_DEFAULT_ACTION_SUBJECT_JABBER', '{STATUS}');
define('STR_DEFAULT_ACTION_TEXT_JABBER', 'Trigger: {TRIGGER.NAME}\nDate / Time: {EVENT.DATE} - {EVENT.TIME}\nHostname: {HOSTNAME}\nComment: {TRIGGER.COMMENT}');
/**
*
* @param <type> $hostid
* @return <type>
*/
function zabbix_bridge_drupal_to_zabbix_hostid($hostid) {
$sql = "select zabbixhostid from {zabbix_hosts} where hostid = %d";
$result = db_query($sql, $hostid);
$host = db_fetch_object($result);
//zabbix_bridge_debug(print_r($host, TRUE));
return $host->zabbixhostid;
}
/**
*
* @global <type> $user
* @param <type> $userid
* @return string
*/
function zabbix_hosts_table($userid = NULL) {
global $user;
$rows = array();
$count = PAGER_COUNT;
$id = TABLE_ID_HOSTS_MAPPING;
if (isset($userid)) {
$header = array('Hostname', 'Enabled', 'Role name', 'Role Description', 'Actions');
$results = pager_query("select h.hostid, h.hostname, h.zabbixhostid, h.enabled, r.name as role_name, r.description as role_desc from {zabbix_hosts} h left join {zabbix_hosts_roles} hr on hr.hostid = h.hostid left join {zabbix_role} r on r.roleid = hr.roleid where h.userid = '%s' order by h.hostname, r.name", $count, $id, null, $user->uid);
}
else {
$header = array('Username', 'Email', 'Zabbix Hostid', 'Hostname', 'Enabled', 'Role name', 'Role Description', 'Actions');
$results = pager_query("select u.name, u.mail, h.hostid, h.zabbixhostid, h.hostname, h.enabled, r.name as role_name, r.description as role_desc from {zabbix_hosts} h left join {zabbix_hosts_roles} hr on hr.hostid = h.hostid left join {zabbix_role} r on r.roleid = hr.roleid left join {users} u on u.uid = h.userid order by u.name, h.hostname, r.name", $count, $id);
}
$lastzabbixhostid = -1;
while ($node = db_fetch_object($results)) {
if (!isset($userid)) {
$rows[] = array(
$node->name,
$node->mail,
$lastzabbixhostid == $node->zabbixhostid ? '' : $node->zabbixhostid,
$lastzabbixhostid == $node->zabbixhostid ? '' : $node->hostname,
$lastzabbixhostid == $node->zabbixhostid ? '' : ($node->enabled == 0 ? 'enabled' : 'disabled'),
$node->role_name,
$node->role_desc,
$lastzabbixhostid == $node->zabbixhostid ? '' : (
l($node->enabled == 0 ? 'Disable' : 'Enable', 'hosts/enable-disable/' . $node->hostid) . ' | ' .
l('Delete', 'hosts/delete/' . $node->hostid) . ' | ' .
l('Update', 'hosts/update/' . $node->hostid) . ' | ' .
l('Triggers', 'hosts/' . $node->hostid . '/triggers')
),
);
}
else {
$rows[] = array(
$lastzabbixhostid == $node->zabbixhostid ? '' : $node->hostname,
$lastzabbixhostid == $node->zabbixhostid ? '' : ($node->enabled == 0 ? 'enabled' : 'disabled'),
$node->role_name,
$node->role_desc,
$lastzabbixhostid == $node->zabbixhostid ? '' : (
l($node->enabled == 0 ? 'Disable' : 'Enable', 'hosts/enable-disable/' . $node->hostid) . ' | ' .
l('Delete', 'hosts/delete/' . $node->hostid) . ' | ' .
l('Update', 'hosts/update/' . $node->hostid) . ' | ' .
l('Triggers', 'hosts/' . $node->hostid . '/triggers')
),
);
}
$lastzabbixhostid = $node->zabbixhostid;
}
if ($lastzabbixhostid == -1) {
if (!isset($userid)) {
$rows[] = array(t('No Hosts have been defined yet.'), '', '', '', '', '', '', '',);
}
else {
$rows[] = array(t('No Hosts have been defined yet.'), '', '', '', '');
}
}
$table_attributes = array('id' => 'hosts-table', 'align' => 'center');
$output = theme('table', $header, $rows, $table_attributes) . theme('pager', $count, $id);
return $output;
}
/**
* thsi function outputs all the tables for the role mappings, one for each roletype
*/
function zabbix_roletype_tables() {
$output = '';
// get all role types from the db
$sql = "select typeid, typename from {zabbix_role_type} order by sortorder";
$result = db_query($sql);
while ($record = db_fetch_object($result)) {
$output .= "<h3>$record->typename</h3>";
$output .= zabbix_roles_table($record->typeid);
}
return $output;
}
/**
* returns the table with all roles for the sepcified roletypeid
* @param <type> $typeid
* @return string
*/
function zabbix_roles_table($typeid) {
$header = array('Roleid', 'Name', 'Description', 'Image', 'Zabbix Templateid', 'Actions');
$rows = array();
$count = PAGER_COUNT;
$id = TABLE_ID_USER_MAPPING;
$results = pager_query("select zr.*, zrt.templateid from {zabbix_role} zr left join {zabbix_roles_templates} zrt on zrt.roleid = zr.roleid where zr.typeid = $typeid order by zr.sortorder", $count, $id);
while ($node = db_fetch_object($results)) {
// TODO: this shouldn't be static!
$imagesrc = $node->imagesrc == NULL ? url('/sites/default/files/zabbix-roles/none.png') : url($node->imagesrc);
$rows[] = array(
$node->roleid,
$node->name,
$node->description,
'<img src="'. $imagesrc ."\" alt=\"$imagesrc\" />",
$node->templateid,
l('Update', 'zabbix-role-mapping/update/' . $node->roleid) . ' | ' .
l('Delete', 'zabbix-role-mapping/delete/' . $node->roleid),
);
}
if (count($rows) == 0) {
$rows[] = array(t('No templates have been defined yet'), '', '', '', '', '');
}
$table_attributes = array('id' => 'roles-table', 'align' => 'center');
$output = theme('table', $header, $rows, $table_attributes) . theme('pager', $count, $id);
return $output;
}
/**
*
* @return string
*/
function zabbix_user_mapping_table() {
$header = array('Username', 'Email', 'Zabbix Userid', 'Zabbix Usergroupid', 'Zabbix Hostgroupid', 'Actions');
$rows = array();
$count = PAGER_COUNT;
$id = TABLE_ID_USER_MAPPING;
$results = pager_query("select zda.id, du.uid, du.name, du.mail, zda.zabbix_uid, zda.zabbix_usrgrp_id, zda.zabbix_hostgrp_id from {zabbix_drupal_account} zda left join {users} du on du.uid = zda.drupal_uid order by du.name", $count, $id);
while ($node = db_fetch_object($results)) {
$rows[] = array(
$node->name,
$node->mail,
$node->zabbix_uid,
$node->zabbix_usrgrp_id,
$node->zabbix_hostgrp_id,
l('Update', 'zabbix-user-mapping/update/' . $node->id) . ' | ' .
l('Delete', 'zabbix-user-mapping/delete/' . $node->id) . ' | ' .
l('Generate missing zabbix items', 'zabbix-user-mapping/generate/' . $node->id),
);
}
if (count($rows) == 0) {
$rows[] = array(t('No mappings have been defined yet'), '', '', '', '', '');
}
$table_attributes = array('id' => 'mapping-table', 'align' => 'center');
$output = theme('table', $header, $rows, $table_attributes) . theme('pager', $count, $id);
return $output;
}
/**
*
* @return <type>
*/
function zabbix_api_login() {
// Get the credentials configured in this module
$API_url = variable_get('zabbix_bridge_API_url', NULL);
$API_user = variable_get('zabbix_bridge_API_user', NULL);
$API_pass = variable_get('zabbix_bridge_API_pass', NULL);
if (!isset($API_url, $API_user, $API_pass)) {
drupal_set_message('Unable to login. API credentials must be set in module setting before using this module!', DRUPAL_MSG_TYPE_ERR);
}
// This logs into Zabbix, and returns FALSE if it fails
return ZabbixAPI::login($API_url, $API_user, $API_pass);
}
/**
* for printing out debugging info, use this instead of echo and what not.
*/
function zabbix_bridge_debug($msg, $verbosemsg = '') {
// print the message only if debug is enabled!
$show_verbose = FALSE;
if ("yes" == variable_get('zabbix_bridge_debug', 'no')) {
if ("yes" == variable_get('zabbix_bridge_debug_verbose', 'no')) {
if ($verbosemsg && strlen($verbosemsg)) {
$show_verbose = TRUE;
}
}
if ($show_verbose) {
drupal_set_message($msg . "\n Extensive msg: \n" . $verbosemsg, DRUPAL_MSG_TYPE_STATUS);
}
else {
drupal_set_message($msg, DRUPAL_MSG_TYPE_STATUS);
}
}
}
function zabbix_bridge_get_all_hostgroupids_from_drupal($zabbixuserid = null) {
$hostgroupids = array();
if (isset($zabbixuserid)) {
$sqlwhere = 'zabbix_uid = '.$zabbixuserid;
} else {
$sqlwhere = 'zabbix_hostgrp_id is not null';
}
$sql = "select zabbix_hostgrp_id from {zabbix_drupal_account} where ".$sqlwhere;
$result = db_query($sql);
while ($node = db_fetch_object($result)) {
$hostgroupids[] = $node->zabbix_hostgrp_id;
}
return $hostgroupids;
}
function zabbix_bridge_get_all_templateids_from_zabbix($extended = FALSE) {
$zabbixtemplateids = array();
$templateids = array();
// This logs into Zabbix, and returns FALSE if it fails
zabbix_api_login()
or drupal_set_message('Unable to login: ' . print_r(ZabbixAPI::getLastError(), TRUE), DRUPAL_MSG_TYPE_ERR);
if ($extended) {
$zabbixtemplateids = ZabbixAPI::fetch_array('template', 'get', array('output' => array('host')));
}
else {
$zabbixtemplateids = ZabbixAPI::fetch_array('template', 'get');
}
foreach ($zabbixtemplateids as $key => $value) {
if ($extended) {
$templateids[] = array('templateid' => $key, 'name' => $value['host']);
}
else {
$templateids[] = $key;
}
}
return $templateids;
}
/**
*
* @return array
*/
function zabbix_bridge_get_all_hosts($withtemplates = TRUE) {
$hosts = array();
$zabbixresult = array();
// This logs into Zabbix, and returns FALSE if it fails
zabbix_api_login()
or drupal_set_message('Unable to login: ' . print_r(ZabbixAPI::getLastError(), TRUE), DRUPAL_MSG_TYPE_ERR);
$hostgroupids = array();
$hostgroupids = zabbix_bridge_get_all_hostgroupids_from_drupal();
// This is a workaround. Zabbxi API call host.get will nto return the list
// of templates for a host unless you ask for all hosts that are in a list
// of templates.
// Therefore, we get a list of all templateids, and ask for all hosts with
// those templates. Note that the real selection criteria is the hostgroups
// we have just retrieved on the drupal side.
$alltemplateids = array();
$alltemplateids = zabbix_bridge_get_all_templateids_from_zabbix();
$zabbixresult = ZabbixAPI::fetch_array('host', 'get', array(
'output' => array('hostid', 'host', 'status'),
'groupids' => $hostgroupids,
'templateids' => $alltemplateids));
foreach ($zabbixresult as $value) {
if ($withtemplates) {
$templateids = array();
if ($value['templates']) {
foreach ($value['templates'] as $template) {
$templateids[] = $template['templateid'];
}
$hosts[] = array('hostid' => $value['hostid'],
'name' => $value['host'],
'enabled' => $value['status'],
'hostgroupid' => $value['groups'][0]['groupid'],
'templateids' => $templateids);
}
}
else {
$hosts[] = $value['hostid'];
}
}
return $hosts;
}
/**
* gets the pretty drupal rolename for display, retrieved by a zabbix templateid
*
* @param integer $templateid the zabbix templateid
*/
function zabbix_bridge_get_rolename_by_templateid($templateid) {
$sql = "select zr.name from {zabbix_role} zr inner join {zabbix_roles_templates} zrt on zrt.roleid = zr.roleid where zrt.templateid = %d";
$result = db_query($sql, $templateid);
$role = db_fetch_object($result);
return $role->name;
}
/**
* Get a template by a triggerid. okay, the way this works: we only have
* triggers on template level, which means that any trigger can only have
* items in it from that trigger. Therefore, getting the template a host-level
* trigger comes from is jut a matter of getting the trigger.templateid, which
* points to the triggerid at template level. For that trigger, we get all
* functions. Those give us the itemids involved in this trigger, so we get the
* hostid from the first itemid in the first function and look up it's hostid.
* That is teh template this trigger belongs to.
*
* @param integer $triggerid the id of the trigger we want to use to fetch it's
* template
*/
function zabbix_bridge_get_template_by_triggerid($triggerid) {
// This logs into Zabbix, and returns false if it fails
zabbix_api_login()
or drupal_set_message('Unable to login: ' . print_r(ZabbixAPI::getLastError(), TRUE), DRUPAL_MSG_TYPE_ERR);
$trigger = array();
$options = array();
$options['triggerids'] = array($triggerid);
$options['output'] = API_OUTPUT_EXTEND;
// $options['select_functions'] = API_OUTPUT_EXTEND;
$options['select_hosts'] = API_OUTPUT_EXTEND;
// $options['select_items'] = API_OUTPUT_EXTEND;
$options['extendoutput'] = 'true';
$trigger = ZabbixAPI::fetch_array('trigger', 'get', $options)
or drupal_set_message('Unable to get zabbix triggers: ' . serialize(ZabbixAPI::getLastError(), TRUE), DRUPAL_MSG_TYPE_ERR);
return $trigger[0]['hosts'][0];
}
/**
* Get a drupal userid by a zabbix hostgroupid. The reason this is even possible
* is because one user owns a hostgroup, making this a 1:1 relation
*
* @param integer $hostgroupid
* @return integer the drupal userid for this hostgroup
*/
function zabbix_bridge_get_userid_by_hostgroupid($hostgroupid) {
$sql = "select drupal_uid from {zabbix_drupal_account} where zabbix_hostgrp_id = %d";
$result = db_query($sql, $hostgroupid);
$user = db_fetch_object($result);
return $user->drupal_uid;
}
/**
*
* @param int $zabbixuserid
* @param int $zabbixhostid
* @param string $zabbixhostname
* @param int $zabbixhostenabled
* @param array $zabbixhosttemplateids
*/
function zabbix_bridge_add_host_to_zabbix_userid(
$zabbixuserid, $zabbixhostid, $zabbixhostname, $zabbixhostenabled, $zabbixhosttemplateids) {
$sql = "insert into {zabbix_hosts} (userid, zabbixhostid, hostname, enabled) values (%d, %d, '%s', '%s')";
$result = db_query($sql, $zabbixuserid, $zabbixhostid, $zabbixhostname, $zabbixhostenabled);
if ($result)
$hostid = db_last_insert_id('zabbix_hosts', 'hostid');
foreach ($zabbixhosttemplateids as $value) {
$sql = "select roleid from {zabbix_roles_templates} where templateid = %d";
$result = db_query($sql, $value);
$role = db_fetch_object($result);
$sql = "insert into {zabbix_hosts_roles} (hostid, roleid) values (%d, %d)";
$result = db_query($sql, $hostid, $role->roleid);
}
}
/**
*
* @param <type> $emailid
* @return <type>
*/
function zabbix_bridge_drupal_to_zabbix_emailid($emailid) {
$sql = "select zabbixmediaid from {zabbix_emails} where emailid = %d";
$result = db_query($sql, $emailid);
$email = db_fetch_object($result);
zabbix_bridge_debug(print_r($email, TRUE));
return $email->zabbixmediaid;
}
/**
*
* @global <type> $user
* @param <type> $userid
* @return string
*/
function zabbix_emails_table($userid = NULL) {
global $user;
$rows = array();
$count = PAGER_COUNT;
$id = TABLE_ID_EMAILS_MAPPING;
if (isset($userid)) {
$header = array('Email', 'Severity', 'Enabled');
$results = pager_query("select e.emailid, e.email, e.zabbixmediaid, zs.name severity, e.enabled from {zabbix_emails} e left join {zabbix_emails_severities} zes on e.emailid = zes.emailid left join {zabbix_severities} zs on zes.severityid = zs.severityid where e.userid = %d", $count, $id, null, $user->uid);
}
else {
$header = array('Username', 'Email Id', 'Email', 'Zabbix Media Id', 'Severity', 'Enabled');
$results = pager_query("select u.name, e.emailid, e.email, e.zabbixmediaid, zs.name severity, e.enabled from {zabbix_emails} e left join {zabbix_emails_severities} zes on e.emailid = zes.emailid left join {zabbix_severities} zs on zes.severityid = zs.severityid left join {users} u on u.uid = e.userid", $count, $id);
}
$lastzabbixmediaid = -1;
while ($node = db_fetch_object($results)) {
if (!isset($userid)) {
$rows[] = array(
$node->name,
$node->emailid,
$node->email,
$lastzabbixmediaid == $node->zabbixmediaid ? '' : $node->zabbixmediaid,
$node->severity,
$lastzabbixmediaid == $node->zabbixmediaid ? '' : ($node->enabled == 0 ? 'enabled' : 'disabled'),
$lastzabbixmediaid == $node->zabbixmediaid ? '' : (
l($node->enabled == 0 ? 'Disable' : 'Enable', 'emails/enable-disable/' . $node->emailid) . ' | ' .
l('Delete', 'emails/delete/' . $node->emailid) . ' | ' .
l('Update', 'emails/update/' . $node->emailid)
),
);
}
else {
$rows[] = array(
$lastzabbixmediaid == $node->zabbixmediaid ? '' : $node->email,
$node->severity,
$lastzabbixmediaid == $node->zabbixmediaid ? '' : ($node->enabled == 0 ? 'enabled' : 'disabled'),
$lastzabbixmediaid == $node->zabbixmediaid ? '' : (
l($node->enabled == 0 ? 'Disable' : 'Enable', 'emails/enable-disable/' . $node->emailid) . ' | ' .
l('Delete', 'emails/delete/' . $node->emailid) . ' | ' .
l('Update', 'emails/update/' . $node->emailid)
),
);
}
$lastzabbixmediaid = $node->zabbixmediaid;
}
if ($lastzabbixmediaid == -1) {
if (!isset($userid)) {
$rows[] = array(t('No Emails have been defined yet.'), '', '', '', '', '');
}
else {
$rows[] = array(t('No Emails have been defined yet.'), '', '', '');
}
}
$table_attributes = array('id' => 'emails-table', 'align' => 'center');
$output = theme('table', $header, $rows, $table_attributes) . theme('pager', $count, $id);
return $output;
}
/**
*
* @param <type> $severities
* @return int
*/
function zabbix_bridge_calculate_severity($severities) {
// if the supplied param is not an array, make it one!
if (!is_array($severities)) {
$tmp = $severities;
$severities = array();
$severities[] = $tmp;
}
// Get the severities from the database
$sql = 'select value from {zabbix_severities} where severityid in (' . db_placeholders($severities, 'int') . ')';
$result = db_query($sql, $severities);
// $severity needs to be a flag, add the power of 2 to the (zabbix-id)'th to the desired severity levels
$severity = 0;
while ($rs = db_fetch_array($result)) {
$severity += pow(2, $rs['value']);
}
return $severity;
}
function zabbix_bridge_get_all_emails_from_zabbix($extended = false) {
$zabbixmedia = array();
$media = array();
// This logs into Zabbix, and returns false if it fails
zabbix_api_login()
or drupal_set_message('Unable to login: ' . print_r(ZabbixAPI::getLastError(), TRUE), DRUPAL_MSG_TYPE_ERR);
$zabbixmedia = ZabbixAPI::fetch_array('user', 'getMedia', array( 'extendoutput' => $extended, 'mediatypeid' => 1 ));
foreach ($zabbixmedia as $key => $value) {
if ($extended) {
$media[] = $value;
}
else {
$media[] = $key;
}
}
return $media;
}
/**
*
* @param <type> $mobileid
* @return <type>
*/
function zabbix_bridge_drupal_to_zabbix_mobileid($mobileid) {
$sql = "select zabbixmediaid from {zabbix_mobiles} where mobileid = '%s'";
$result = db_query($sql, $mobileid);
$mobile = db_fetch_object($result);
zabbix_bridge_debug(print_r($mobile, TRUE));
return $mobile->zabbixmediaid;
}
/**
*
* @param integer $triggerid
* the zabbix id of the trigger to disable/enable
* @param string $action
* the action to take 'enable' or 'disable'
*/
function zabbix_bridge_trigger_toggle($triggerid, $action) {
zabbix_api_login()
or drupal_set_message('Unable to login. ' . print_r(ZabbixAPI::getLastError(), TRUE), DRUPAL_MSG_TYPE_ERR);
$update = array();
$update[] = array('triggerid' => $triggerid, 'status' => (($action == 'enable') ? 0 : 1));
$result = ZabbixAPI::fetch_string('trigger', 'update', $update);
zabbix_bridge_debug(print_r($triggerid, TRUE));
zabbix_bridge_debug(print_r($action, TRUE));
if (!$result) {
drupal_set_message('Unable to enable/disable trigger. ' . print_r(ZabbixAPI::getLastError(), TRUE), DRUPAL_MSG_TYPE_ERR);
}
else {
drupal_set_message('Trigger succesfully updated. ', DRUPAL_MSG_TYPE_STATUS);
}
return $result;
}
/**
*
* @global <type> $user
* @param <type> $userid
* @return string
*/
function zabbix_mobiles_table($userid = NULL) {
global $user;
$rows = array();
$count = PAGER_COUNT;
$id = TABLE_ID_EMAILS_MAPPING;
if (isset($userid)) {
$header = array('Mobile', 'Severity', 'Enabled');
$results = pager_query("select m.mobileid, m.number, m.zabbixmediaid, zs.name severity, m.enabled from {zabbix_mobiles} m left join {zabbix_mobiles_severities} zms on m.mobileid = zms.mobileid left join {zabbix_severities} zs on zms.severityid = zs.severityid where m.userid = '%s'", $count, $id, null, $user->uid);
}
else {
$header = array('Username', 'Mobile Id', 'Mobile Number', 'Zabbix Media Id', 'Severity', 'Enabled');
$results = pager_query("select u.name, m.mobileid, m.number, m.zabbixmediaid, zs.name severity, m.enabled from {zabbix_mobiles} m left join {zabbix_mobiles_severities} zms on m.mobileid = zms.mobileid left join {zabbix_severities} zs on zms.severityid = zs.severityid left join {users} u on u.uid = m.userid", $count, $id);
}
$lastzabbixmediaid = -1;
while ($node = db_fetch_object($results)) {
if (!isset($userid)) {
$rows[] = array(
$node->name,
$node->mobileid,
$node->number,
$lastzabbixmediaid == $node->zabbixmediaid ? '' : $node->zabbixmediaid,
$node->severity,
$lastzabbixmediaid == $node->zabbixmediaid ? '' : ($node->enabled == 0 ? 'enabled' : 'disabled'),
$lastzabbixmediaid == $node->zabbixmediaid ? '' : (
l($node->enabled == 0 ? 'Disable' : 'Enable', 'mobiles/enable-disable/' . $node->mobileid) . ' | ' .
l('Delete', 'mobiles/delete/' . $node->mobileid) . ' | ' .
l('Update', 'mobiles/update/' . $node->mobileid)
),
);
}
else {
$rows[] = array(
$lastzabbixmediaid == $node->zabbixmediaid ? '' : $node->number,
$node->severity,
$lastzabbixmediaid == $node->zabbixmediaid ? '' : ($node->enabled == 0 ? 'enabled' : 'disabled'),
$lastzabbixmediaid == $node->zabbixmediaid ? '' : (
l($node->enabled == 0 ? 'Disable' : 'Enable', 'mobiles/enable-disable/' . $node->mobileid) . ' | ' .
l('Delete', 'mobiles/delete/' . $node->mobileid) . ' | ' .
l('Update', 'mobiles/update/' . $node->mobileid)
),
);
}
$lastzabbixmediaid = $node->zabbixmediaid;
}
if ($lastzabbixmediaid == -1) {
if (!isset($userid)) {
$rows[] = array(t('No mobile numbers have been defined yet.'), '', '', '', '', '');
}
else {
$rows[] = array(t('No mobile numbers have been defined yet.'), '', '', '');
}
}
$table_attributes = array('id' => 'mobiles-table', 'align' => 'center');
$output = theme('table', $header, $rows, $table_attributes) . theme('pager', $count, $id);
return $output;
}
function zabbix_bridge_get_all_mobiles_from_zabbix($extended = false) {
$zabbixmedia = array();
$media = array();
// This logs into Zabbix, and returns false if it fails
zabbix_api_login()
or drupal_set_message('Unable to login: ' . print_r(ZabbixAPI::getLastError(), TRUE), DRUPAL_MSG_TYPE_ERR);
$zabbixmedia = ZabbixAPI::fetch_array('user', 'getMedia', array( 'extendoutput' => $extended, 'mediatypeid' => 3 ));
foreach ($zabbixmedia as $key => $value) {
if ($extended) {
$media[] = $value;
}
else {
$media[] = $key;
}
}
return $media;
}
function zabbix_bridge_get_trigger_by_triggerid($triggerid) {
$trigger = array();
// This logs into Zabbix, and returns false if it fails
zabbix_api_login()
or drupal_set_message('Unable to login: ' . print_r(ZabbixAPI::getLastError(), TRUE), DRUPAL_MSG_TYPE_ERR);
$trigger = ZabbixAPI::fetch_array('trigger', 'get', array( 'triggerid' => $triggerid));
return $trigger;
}
/**
*
* @param <type> $jabberid
* @return <type>
*/
function zabbix_bridge_drupal_to_zabbix_jabberid($jabberid) {
$sql = "select zabbixmediaid from {zabbix_jabbers} where jabberid = '%s'";
$result = db_query($sql, $jabberid);
$jabber = db_fetch_object($result);
zabbix_bridge_debug(print_r($jabber, TRUE));
return $jabber->zabbixmediaid;
}
/**
*
* @global <type> $user
* @param <type> $userid
* @return string
*/
function zabbix_jabbers_table($userid = NULL) {
global $user;
$rows = array();
$count = PAGER_COUNT;
$id = TABLE_ID_EMAILS_MAPPING;
if (isset($userid)) {
$header = array('Jabber', 'Severity', 'Enabled');
$results = pager_query("select j.jabberid, j.jabber, j.zabbixmediaid, zs.name severity, j.enabled from {zabbix_jabbers} j left join {zabbix_jabbers_severities} zjs on j.jabberid = zjs.jabberid left join {zabbix_severities} zs on zjs.severityid = zs.severityid where j.userid = '%s'", $count, $id, null, $user->uid);
}
else {
$header = array('Username', 'Jabber Id', 'Jabber', 'Zabbix Media Id', 'Severity', 'Enabled');
$results = pager_query("select u.name, j.jabberid, j.jabber, j.zabbixmediaid, zs.name severity, j.enabled from {zabbix_jabbers} j left join {zabbix_jabbers_severities} zjs on j.jabberid = zjs.jabberid left join {zabbix_severities} zs on zjs.severityid = zs.severityid left join {users} u on u.uid = j.userid", $count, $id);
}
$lastzabbixmediaid = -1;
while ($node = db_fetch_object($results)) {
if (!isset($userid)) {
$rows[] = array(
$node->name,
$node->jabberid,
$node->jabber,
$lastzabbixmediaid == $node->zabbixmediaid ? '' : $node->zabbixmediaid,
$node->severity,
$lastzabbixmediaid == $node->zabbixmediaid ? '' : ($node->enabled == 0 ? 'enabled' : 'disabled'),
$lastzabbixmediaid == $node->zabbixmediaid ? '' : (
l($node->enabled == 0 ? 'Disable' : 'Enable', 'jabbers/enable-disable/' . $node->jabberid) . ' | ' .
l('Delete', 'jabbers/delete/' . $node->jabberid) . ' | ' .
l('Update', 'jabbers/update/' . $node->jabberid)
),
);
}
else {
$rows[] = array(
$lastzabbixmediaid == $node->zabbixmediaid ? '' : $node->jabber,
$node->severity,
$lastzabbixmediaid == $node->zabbixmediaid ? '' : ($node->enabled == 0 ? 'enabled' : 'disabled'),
$lastzabbixmediaid == $node->zabbixmediaid ? '' : (
l($node->enabled == 0 ? 'Disable' : 'Enable', 'jabbers/enable-disable/' . $node->jabberid) . ' | ' .
l('Delete', 'jabbers/delete/' . $node->jabberid) . ' | ' .
l('Update', 'jabbers/update/' . $node->jabberid)
),
);
}
$lastzabbixmediaid = $node->zabbixmediaid;
}
if ($lastzabbixmediaid == -1) {
if (!isset($userid)) {
$rows[] = array(t('No jabber ids have been defined yet.'), '', '', '', '', '');
}