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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
|
/*
** ZABBIX
** Copyright (C) 2000-2005 SIA Zabbix
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/
#include <stdlib.h>
#include <stdio.h>
/* for setproctitle() */
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include "db.h"
#include "log.h"
#include "zlog.h"
#include "common.h"
#ifdef HAVE_MYSQL
MYSQL mysql;
#endif
#ifdef HAVE_PGSQL
PGconn *conn;
#endif
#ifdef HAVE_ORACLE
sqlo_db_handle_t oracle;
#endif
extern void apply_actions(DB_TRIGGER *trigger,int alarmid,int trigger_value);
extern void update_services(int triggerid, int status);
void DBclose(void)
{
#ifdef HAVE_MYSQL
mysql_close(&mysql);
#endif
#ifdef HAVE_PGSQL
PQfinish(conn);
#endif
#ifdef HAVE_ORACLE
sqlo_finish(oracle);
#endif
}
/*
* Connect to the database.
* If fails, program terminates.
*/
void DBconnect(void)
{
/* zabbix_log(LOG_LEVEL_ERR, "[%s] [%s] [%s]\n",dbname, dbuser, dbpassword ); */
#ifdef HAVE_MYSQL
/* For MySQL >3.22.00 */
/* if( ! mysql_connect( &mysql, NULL, dbuser, dbpassword ) )*/
mysql_init(&mysql);
if( ! mysql_real_connect( &mysql, CONFIG_DBHOST, CONFIG_DBUSER, CONFIG_DBPASSWORD, CONFIG_DBNAME, CONFIG_DBPORT, CONFIG_DBSOCKET,0 ) )
{
zabbix_log(LOG_LEVEL_ERR, "Failed to connect to database: Error: %s",mysql_error(&mysql) );
exit(FAIL);
}
else
{
if( mysql_select_db( &mysql, CONFIG_DBNAME ) != 0 )
{
zabbix_log(LOG_LEVEL_ERR, "Failed to select database: Error: %s",mysql_error(&mysql) );
exit(FAIL);
}
}
#endif
#ifdef HAVE_PGSQL
/* conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName); */
/* conn = PQsetdb(NULL, NULL, NULL, NULL, dbname);*/
conn = PQsetdbLogin(CONFIG_DBHOST, NULL, NULL, NULL, CONFIG_DBNAME, CONFIG_DBUSER, CONFIG_DBPASSWORD );
/* check to see that the backend connection was successfully made */
if (PQstatus(conn) != CONNECTION_OK)
{
zabbix_log(LOG_LEVEL_ERR, "Connection to database '%s' failed.\n", CONFIG_DBNAME);
exit(FAIL);
}
#endif
#ifdef HAVE_ORACLE
if (SQLO_SUCCESS != sqlo_init(SQLO_OFF, 1, 100))
{
zabbix_log(LOG_LEVEL_ERR, "Failed to init libsqlora8");
exit(FAIL);
}
/* login */
if (SQLO_SUCCESS != sqlo_connect(&oracle, "scott/tiger"))
{
printf("Cannot login with %s\n", "scott/tiger");
zabbix_log(LOG_LEVEL_ERR, "Cannot login with %s\n", "scott/tiger");
exit(FAIL);
}
#endif
}
/*
* Execute SQL statement. For non-select statements only.
* If fails, program terminates.
*/
int DBexecute(char *query)
{
/* Do not include any code here. Will break HAVE_PGSQL section */
#ifdef HAVE_MYSQL
/* if(strstr(query, "17828") != NULL)*/
zabbix_log( LOG_LEVEL_DEBUG, "Executing query:%s",query);
while( mysql_query(&mysql,query) != 0)
{
zabbix_log( LOG_LEVEL_ERR, "Query::%s",query);
zabbix_log(LOG_LEVEL_ERR, "Query failed:%s [%d]", mysql_error(&mysql), mysql_errno(&mysql) );
if( (ER_SERVER_SHUTDOWN != mysql_errno(&mysql)) &&
(CR_SERVER_GONE_ERROR != mysql_errno(&mysql)) &&
(CR_CONNECTION_ERROR != mysql_errno(&mysql)))
{
return FAIL;
}
DBclose();
DBconnect();
}
#endif
#ifdef HAVE_PGSQL
PGresult *result;
zabbix_log( LOG_LEVEL_DEBUG, "Executing query:%s",query);
result = PQexec(conn,query);
if( result==NULL)
{
zabbix_log( LOG_LEVEL_ERR, "Query::%s",query);
zabbix_log(LOG_LEVEL_ERR, "Query failed:%s", "Result is NULL" );
PQclear(result);
return FAIL;
}
if( PQresultStatus(result) != PGRES_COMMAND_OK)
{
zabbix_log( LOG_LEVEL_ERR, "Query::%s",query);
zabbix_log(LOG_LEVEL_ERR, "Query failed:%s", PQresStatus(PQresultStatus(result)) );
PQclear(result);
return FAIL;
}
PQclear(result);
#endif
#ifdef HAVE_ORACLE
if ( sqlo_exec(oracle, query)<0 )
{
zabbix_log( LOG_LEVEL_ERR, "Query::%s",query);
zabbix_log(LOG_LEVEL_ERR, "Query failed:%s", sqlo_geterror(oracle) );
fprintf(stderr, "Query::%s\n",query);
fprintf(stderr, "Query failed:%s\n", sqlo_geterror(oracle) );
return FAIL;
}
#endif
/* zabbix_set_log_level(LOG_LEVEL_WARNING);*/
return SUCCEED;
}
int DBis_null(char *field)
{
int ret = FAIL;
if(field == NULL) ret = SUCCEED;
#ifdef HAVE_ORACLE
else if(field[0] == 0) ret = SUCCEED;
#endif
return ret;
}
DB_ROW DBfetch(DB_RESULT result)
{
#ifdef HAVE_MYSQL
return mysql_fetch_row(result);
#endif
#ifdef HAVE_ORACLE
int res;
res = sqlo_fetch(result, 1);
if(SQLO_SUCCESS == res)
{
return sqlo_values(result, NULL, 1);
}
else if(SQLO_NO_DATA == res)
{
return 0;
}
else
{
fprintf(stderr, "Fetch failed:%s\n", sqlo_geterror(oracle) );
exit(FAIL);
}
#endif
}
/*
* Execute SQL statement. For select statements only.
* If fails, program terminates.
*/
DB_RESULT DBselect(char *query)
{
/* Do not include any code here. Will break HAVE_PGSQL section */
#ifdef HAVE_MYSQL
zabbix_log( LOG_LEVEL_DEBUG, "Executing query:%s",query);
while(mysql_query(&mysql,query) != 0)
{
zabbix_log( LOG_LEVEL_ERR, "Query::%s",query);
zabbix_log(LOG_LEVEL_ERR, "Query failed:%s [%d]", mysql_error(&mysql), mysql_errno(&mysql) );
if( (ER_SERVER_SHUTDOWN != mysql_errno(&mysql)) &&
(CR_SERVER_GONE_ERROR != mysql_errno(&mysql)) &&
(CR_CONNECTION_ERROR != mysql_errno(&mysql)))
{
exit(FAIL);
}
DBclose();
DBconnect();
}
return mysql_store_result(&mysql);
#endif
#ifdef HAVE_PGSQL
PGresult *result;
zabbix_log( LOG_LEVEL_DEBUG, "Executing query:%s",query);
result = PQexec(conn,query);
if( result==NULL)
{
zabbix_log( LOG_LEVEL_ERR, "Query::%s",query);
zabbix_log(LOG_LEVEL_ERR, "Query failed:%s", "Result is NULL" );
exit( FAIL );
}
if( PQresultStatus(result) != PGRES_TUPLES_OK)
{
zabbix_log( LOG_LEVEL_ERR, "Query::%s",query);
zabbix_log(LOG_LEVEL_ERR, "Query failed:%s", PQresStatus(PQresultStatus(result)) );
exit( FAIL );
}
return result;
#endif
#ifdef HAVE_ORACLE
sqlo_stmt_handle_t sth;
if(0 > (sth = (sqlo_open(oracle, query,0,NULL))))
{
zabbix_log( LOG_LEVEL_ERR, "Query::%s",query);
zabbix_log(LOG_LEVEL_ERR, "Query failed:%s", sqlo_geterror(oracle));
exit(FAIL);
}
return sth;
#endif
}
/*
* Get value for given row and field. Must be called after DBselect.
*/
/*
char *DBget_field(DB_RESULT result, int rownum, int fieldnum)
{
#ifdef HAVE_MYSQL
MYSQL_ROW row;
mysql_data_seek(result, rownum);
row=mysql_fetch_row(result);
if(row == NULL)
{
zabbix_log(LOG_LEVEL_ERR, "Error while mysql_fetch_row():Error [%s] Rownum [%d] Fieldnum [%d]", mysql_error(&mysql), rownum, fieldnum );
zabbix_syslog("MYSQL: Error while mysql_fetch_row():Error [%s] Rownum [%d] Fieldnum [%d]", mysql_error(&mysql), rownum, fieldnum );
exit(FAIL);
}
return row[fieldnum];
#endif
#ifdef HAVE_PGSQL
return PQgetvalue(result, rownum, fieldnum);
#endif
#ifdef HAVE_ORACLE
return FAIL;
#endif
}
*/
/*
* Get value of autoincrement field for last insert or update statement
*/
int DBinsert_id()
{
#ifdef HAVE_MYSQL
zabbix_log(LOG_LEVEL_DEBUG, "In DBinsert_id()" );
return mysql_insert_id(&mysql);
#endif
#ifdef HAVE_PGSQL
#error SUPPORT OF POSTGRESQL NOT IMPLEMENTED YET
#endif
#ifdef HAVE_ORACLE
return FAIL;
#endif
}
/*
* Returs number of affected rows of last select, update, delete or replace
*/
long DBaffected_rows()
{
#ifdef HAVE_MYSQL
/* It actually returns my_ulonglong */
return (long)mysql_affected_rows(&mysql);
#endif
#ifdef HAVE_PGSQL
NOT IMPLEMENTED YET
#endif
#ifdef HAVE_ORACLE
return FAIL;
#endif
}
/*
* Return SUCCEED if result conains no records
*/
/*int DBis_empty(DB_RESULT *result)
{
zabbix_log(LOG_LEVEL_DEBUG, "In DBis_empty");
if(result == NULL)
{
return SUCCEED;
}
if(DBnum_rows(result) == 0)
{
return SUCCEED;
}
if(DBget_field(result,0,0) == 0)
{
return SUCCEED;
}
return FAIL;
}*/
/*
* Get number of selected records.
*/
/*
int DBnum_rows(DB_RESULT result)
{
#ifdef HAVE_MYSQL
int rows;
zabbix_log(LOG_LEVEL_DEBUG, "In DBnum_rows");
if(result == NULL)
{
return 0;
}
// Order is important !
rows = mysql_num_rows(result);
if(rows == 0)
{
return 0;
}
// This is necessary to exclude situations like
// atoi(DBget_field(result,0,0). This leads to coredump.
//
// This is required for empty results for count(*), etc
if(DBget_field(result,0,0) == 0)
{
return 0;
}
zabbix_log(LOG_LEVEL_DEBUG, "Result of DBnum_rows [%d]", rows);
return rows;
#endif
#ifdef HAVE_PGSQL
zabbix_log(LOG_LEVEL_DEBUG, "In DBnum_rows");
return PQntuples(result);
#endif
#ifdef HAVE_ORACLE
return sqlo_prows(result);
#endif
}
*/
/*
* Get function value.
*/
int DBget_function_result(double *result,char *functionid)
{
DB_RESULT dbresult;
DB_ROW row;
int res = SUCCEED;
char sql[MAX_STRING_LEN];
/* 0 is added to distinguish between lastvalue==NULL and empty result */
snprintf( sql, sizeof(sql)-1, "select 0,lastvalue from functions where functionid=%s", functionid );
dbresult = DBselect(sql);
row = DBfetch(dbresult);
if(!row)
{
zabbix_log(LOG_LEVEL_WARNING, "No function for functionid:[%s]", functionid );
zabbix_syslog("No function for functionid:[%s]", functionid );
res = FAIL;
}
else if(DBis_null(row[1]) == SUCCEED)
{
zabbix_log(LOG_LEVEL_DEBUG, "function.lastvalue==NULL [%s]", functionid );
res = FAIL;
}
else
{
*result=atof(row[1]);
}
DBfree_result(dbresult);
return res;
}
/* Returns previous trigger value. If not value found, return TRIGGER_VALUE_FALSE */
int DBget_prev_trigger_value(int triggerid)
{
char sql[MAX_STRING_LEN];
int clock;
int value;
DB_RESULT result;
DB_ROW row;
zabbix_log(LOG_LEVEL_DEBUG,"In DBget_prev_trigger_value[%d]", triggerid);
snprintf(sql,sizeof(sql)-1,"select max(clock) from alarms where triggerid=%d",triggerid);
zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql);
result = DBselect(sql);
row=DBfetch(result);
if(!row)
{
zabbix_log(LOG_LEVEL_DEBUG, "Result for MAX is empty" );
DBfree_result(result);
return TRIGGER_VALUE_UNKNOWN;
}
clock=atoi(row[0]);
DBfree_result(result);
snprintf(sql,sizeof(sql),"select max(clock) from alarms where triggerid=%d and clock<%d",triggerid,clock);
zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql);
result = DBselect(sql);
row=DBfetch(result);
if(!row)
{
zabbix_log(LOG_LEVEL_DEBUG, "Result for MAX is empty" );
DBfree_result(result);
/* Assume that initially Trigger value is False. Otherwise alarms will not be generated when
status changes to TRUE for te first time */
return TRIGGER_VALUE_FALSE;
/* return TRIGGER_VALUE_UNKNOWN;*/
}
clock=atoi(row[0]);
DBfree_result(result);
snprintf(sql,sizeof(sql)-1,"select value from alarms where triggerid=%d and clock=%d",triggerid,clock);
zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql);
result = DBselect(sql);
row=DBfetch(result);
if(!row)
{
zabbix_log(LOG_LEVEL_DEBUG, "Result of [%s] is empty", sql );
DBfree_result(result);
return TRIGGER_VALUE_UNKNOWN;
}
value=atoi(row[0]);
DBfree_result(result);
return value;
}
/* SUCCEED if latest alarm with triggerid has this status */
/* Rewrite required to simplify logic ?*/
static int latest_alarm(int triggerid, int status)
{
char sql[MAX_STRING_LEN];
int clock;
DB_RESULT result;
DB_ROW row;
int ret = FAIL;
zabbix_log(LOG_LEVEL_DEBUG,"In latest_alarm()");
snprintf(sql,sizeof(sql)-1,"select max(clock) from alarms where triggerid=%d",triggerid);
zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql);
result = DBselect(sql);
row = DBfetch(result);
if(!row)
{
zabbix_log(LOG_LEVEL_DEBUG, "Result for MAX is empty" );
ret = FAIL;
}
else
{
clock=atoi(row[0]);
DBfree_result(result);
snprintf(sql,sizeof(sql)-1,"select value from alarms where triggerid=%d and clock=%d",triggerid,clock);
zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql);
result = DBselect(sql);
row = DBfetch(result);
if(row)
{
if(atoi(row[0]) == status)
{
ret = SUCCEED;
}
}
}
DBfree_result(result);
return ret;
}
/* SUCCEED if latest service alarm has this status */
/* Rewrite required to simplify logic ?*/
int latest_service_alarm(int serviceid, int status)
{
char sql[MAX_STRING_LEN];
int clock;
DB_RESULT result;
DB_ROW row;
int ret = FAIL;
zabbix_log(LOG_LEVEL_DEBUG,"In latest_service_alarm()");
snprintf(sql,sizeof(sql)-1,"select max(clock) from service_alarms where serviceid=%d",serviceid);
zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql);
result = DBselect(sql);
row = DBfetch(result);
if(!row)
{
zabbix_log(LOG_LEVEL_DEBUG, "Result for MAX is empty" );
ret = FAIL;
}
else
{
clock=atoi(row[0]);
DBfree_result(result);
snprintf(sql,sizeof(sql)-1,"select value from service_alarms where serviceid=%d and clock=%d",serviceid,clock);
zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql);
result = DBselect(sql);
row = DBfetch(result);
if(row)
{
if(atoi(row[0]) == status)
{
ret = SUCCEED;
}
}
}
DBfree_result(result);
return ret;
}
/* Returns alarmid or 0 */
int add_alarm(int triggerid,int status,int clock,int *alarmid)
{
char sql[MAX_STRING_LEN];
*alarmid=0;
zabbix_log(LOG_LEVEL_DEBUG,"In add_alarm(%d,%d,%d)",triggerid, status, *alarmid);
/* Latest alarm has the same status? */
if(latest_alarm(triggerid,status) == SUCCEED)
{
zabbix_log(LOG_LEVEL_DEBUG,"Alarm for triggerid [%d] status [%d] already exists",triggerid,status);
return FAIL;
}
snprintf(sql,sizeof(sql)-1,"insert into alarms(triggerid,clock,value) values(%d,%d,%d)", triggerid, clock, status);
DBexecute(sql);
*alarmid=DBinsert_id();
/* Cancel currently active escalations */
if(status == TRIGGER_VALUE_FALSE || status == TRIGGER_VALUE_TRUE)
{
zabbix_log(LOG_LEVEL_DEBUG,"Before SQL");
snprintf(sql,sizeof(sql)-1,"update escalation_log set status=1 where triggerid=%d and status=0", triggerid);
zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql);
DBexecute(sql);
}
/* Cancel currently active alerts */
if(status == TRIGGER_VALUE_FALSE || status == TRIGGER_VALUE_TRUE)
{
snprintf(sql,sizeof(sql)-1,"update alerts set retries=3,error='Trigger changed its status. WIll not send repeats.' where triggerid=%d and repeats>0 and status=%d", triggerid, ALERT_STATUS_NOT_SENT);
DBexecute(sql);
}
zabbix_log(LOG_LEVEL_DEBUG,"End of add_alarm()");
return SUCCEED;
}
int DBadd_service_alarm(int serviceid,int status,int clock)
{
char sql[MAX_STRING_LEN];
zabbix_log(LOG_LEVEL_DEBUG,"In add_service_alarm()");
if(latest_service_alarm(serviceid,status) == SUCCEED)
{
return SUCCEED;
}
snprintf(sql,sizeof(sql)-1,"insert into service_alarms(serviceid,clock,value) values(%d,%d,%d)", serviceid, clock, status);
zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql);
DBexecute(sql);
zabbix_log(LOG_LEVEL_DEBUG,"End of add_service_alarm()");
return SUCCEED;
}
int DBupdate_trigger_value(DB_TRIGGER *trigger, int new_value, int now, char *reason)
{
char sql[MAX_STRING_LEN];
int alarmid;
int ret = SUCCEED;
if(reason==NULL)
{
zabbix_log(LOG_LEVEL_DEBUG,"In update_trigger_value[%d,%d,%d]", trigger->triggerid, new_value, now);
}
else
{
zabbix_log(LOG_LEVEL_DEBUG,"In update_trigger_value[%d,%d,%d,%s]", trigger->triggerid, new_value, now, reason);
}
if(trigger->value != new_value)
{
trigger->prevvalue=DBget_prev_trigger_value(trigger->triggerid);
if(add_alarm(trigger->triggerid,new_value,now,&alarmid) == SUCCEED)
{
if(reason==NULL)
{
snprintf(sql,sizeof(sql)-1,"update triggers set value=%d,lastchange=%d,error='' where triggerid=%d",new_value,now,trigger->triggerid);
}
else
{
snprintf(sql,sizeof(sql)-1,"update triggers set value=%d,lastchange=%d,error='%s' where triggerid=%d",new_value,now,reason, trigger->triggerid);
}
DBexecute(sql);
/* It is not required and is wrong! */
/* if(TRIGGER_VALUE_UNKNOWN == new_value)
{
snprintf(sql,sizeof(sql)-1,"update functions set lastvalue=NULL where triggerid=%d",trigger->triggerid);
DBexecute(sql);
}*/
if( ((trigger->value == TRIGGER_VALUE_TRUE) && (new_value == TRIGGER_VALUE_FALSE)) ||
((trigger->value == TRIGGER_VALUE_FALSE) && (new_value == TRIGGER_VALUE_TRUE)) ||
((trigger->prevvalue == TRIGGER_VALUE_FALSE) && (trigger->value == TRIGGER_VALUE_UNKNOWN) && (new_value == TRIGGER_VALUE_TRUE)) ||
((trigger->prevvalue == TRIGGER_VALUE_TRUE) && (trigger->value == TRIGGER_VALUE_UNKNOWN) && (new_value == TRIGGER_VALUE_FALSE)))
{
zabbix_log(LOG_LEVEL_DEBUG,"In update_trigger_value. Before apply_actions. Triggerid [%d]", trigger->triggerid);
apply_actions(trigger,alarmid,new_value);
if(new_value == TRIGGER_VALUE_TRUE)
{
update_services(trigger->triggerid, trigger->priority);
}
else
{
update_services(trigger->triggerid, 0);
}
}
}
else
{
zabbix_log(LOG_LEVEL_DEBUG,"Alarm not added for triggerid [%d]", trigger->triggerid);
ret = FAIL;
}
}
return ret;
}
/*
int DBupdate_trigger_value(int triggerid,int value,int clock)
{
char sql[MAX_STRING_LEN];
zabbix_log(LOG_LEVEL_DEBUG,"In update_trigger_value[%d,%d,%d]", triggerid, value, clock);
add_alarm(triggerid,value,clock);
snprintf(sql,sizeof(sql)-1,"update triggers set value=%d,lastchange=%d where triggerid=%d",value,clock,triggerid);
DBexecute(sql);
if(TRIGGER_VALUE_UNKNOWN == value)
{
snprintf(sql,sizeof(sql)-1,"update functions set lastvalue=NULL where triggerid=%d",triggerid);
DBexecute(sql);
}
zabbix_log(LOG_LEVEL_DEBUG,"End of update_trigger_value()");
return SUCCEED;
}
*/
void update_triggers_status_to_unknown(int hostid,int clock,char *reason)
{
char sql[MAX_STRING_LEN];
DB_RESULT result;
DB_ROW row;
DB_TRIGGER trigger;
zabbix_log(LOG_LEVEL_DEBUG,"In update_triggers_status_to_unknown()");
/* snprintf(sql,sizeof(sql)-1,"select distinct t.triggerid from hosts h,items i,triggers t,functions f where f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid and h.hostid=%d and i.key_<>'%s'",hostid,SERVER_STATUS_KEY);*/
snprintf(sql,sizeof(sql)-1,"select distinct t.triggerid,t.value from hosts h,items i,triggers t,functions f where f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid and h.hostid=%d and i.key_ not in ('%s','%s','%s')",hostid,SERVER_STATUS_KEY, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY);
zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql);
result = DBselect(sql);
while((row=DBfetch(result)))
{
trigger.triggerid=atoi(row[0]);
trigger.value=atoi(row[1]);
DBupdate_trigger_value(&trigger,TRIGGER_VALUE_UNKNOWN,clock,reason);
}
DBfree_result(result);
zabbix_log(LOG_LEVEL_DEBUG,"End of update_triggers_status_to_unknown()");
return;
}
void DBdelete_service(int serviceid)
{
char sql[MAX_STRING_LEN];
snprintf(sql,sizeof(sql)-1,"delete from services_links where servicedownid=%d or serviceupid=%d", serviceid, serviceid);
DBexecute(sql);
snprintf(sql,sizeof(sql)-1,"delete from services where serviceid=%d", serviceid);
DBexecute(sql);
}
void DBdelete_services_by_triggerid(int triggerid)
{
int serviceid;
char sql[MAX_STRING_LEN];
DB_RESULT result;
DB_ROW row;
zabbix_log(LOG_LEVEL_DEBUG,"In DBdelete_services_by_triggerid(%d)", triggerid);
snprintf(sql,sizeof(sql)-1,"select serviceid from services where triggerid=%d", triggerid);
result = DBselect(sql);
while((row=DBfetch(result)))
{
serviceid=atoi(row[0]);
DBdelete_service(serviceid);
}
DBfree_result(result);
zabbix_log(LOG_LEVEL_DEBUG,"End of DBdelete_services_by_triggerid(%d)", triggerid);
}
void DBdelete_trigger(int triggerid)
{
char sql[MAX_STRING_LEN];
snprintf(sql,sizeof(sql)-1,"delete from trigger_depends where triggerid_down=%d or triggerid_up=%d", triggerid, triggerid);
DBexecute(sql);
snprintf(sql,sizeof(sql)-1,"delete from functions where triggerid=%d", triggerid);
DBexecute(sql);
snprintf(sql,sizeof(sql)-1,"delete from alarms where triggerid=%d", triggerid);
DBexecute(sql);
/* snprintf(sql,sizeof(sql)-1,"delete from actions where triggerid=%d and scope=%d", triggerid, ACTION_SCOPE_TRIGGER);
DBexecute(sql);*/
DBdelete_services_by_triggerid(triggerid);
snprintf(sql,sizeof(sql)-1,"update sysmaps_links set triggerid=NULL where triggerid=%d", triggerid);
DBexecute(sql);
snprintf(sql,sizeof(sql)-1,"delete from triggers where triggerid=%d", triggerid);
DBexecute(sql);
}
void DBdelete_triggers_by_itemid(int itemid)
{
int triggerid;
char sql[MAX_STRING_LEN];
DB_RESULT result;
DB_ROW row;
zabbix_log(LOG_LEVEL_DEBUG,"In DBdelete_triggers_by_itemid(%d)", itemid);
snprintf(sql,sizeof(sql)-1,"select triggerid from functions where itemid=%d", itemid);
result = DBselect(sql);
while((row=DBfetch(result)))
{
triggerid=atoi(row[0]);
DBdelete_trigger(triggerid);
}
DBfree_result(result);
snprintf(sql,sizeof(sql)-1,"delete from functions where itemid=%d", itemid);
DBexecute(sql);
zabbix_log(LOG_LEVEL_DEBUG,"End of DBdelete_triggers_by_itemid(%d)", itemid);
}
void DBdelete_trends_by_itemid(int itemid)
{
char sql[MAX_STRING_LEN];
snprintf(sql,sizeof(sql)-1,"delete from trends where itemid=%d", itemid);
DBexecute(sql);
}
void DBdelete_history_by_itemid(int itemid)
{
char sql[MAX_STRING_LEN];
snprintf(sql,sizeof(sql)-1,"delete from history where itemid=%d", itemid);
DBexecute(sql);
snprintf(sql,sizeof(sql)-1,"delete from history_str where itemid=%d", itemid);
DBexecute(sql);
}
void DBdelete_sysmaps_links_by_shostid(int shostid)
{
char sql[MAX_STRING_LEN];
snprintf(sql,sizeof(sql)-1,"delete from sysmaps_links where shostid1=%d or shostid2=%d", shostid, shostid);
DBexecute(sql);
}
void DBdelete_sysmaps_hosts_by_hostid(int hostid)
{
int shostid;
char sql[MAX_STRING_LEN];
DB_RESULT result;
DB_ROW row;
zabbix_log(LOG_LEVEL_DEBUG,"In DBdelete_sysmaps_hosts(%d)", hostid);
snprintf(sql,sizeof(sql)-1,"select shostid from sysmaps_hosts where hostid=%d", hostid);
result = DBselect(sql);
while((row=DBfetch(result)))
{
shostid=atoi(row[0]);
DBdelete_sysmaps_links_by_shostid(shostid);
}
DBfree_result(result);
snprintf(sql,sizeof(sql)-1,"delete from sysmaps_hosts where hostid=%d", hostid);
DBexecute(sql);
}
int DBdelete_history_pertial(int itemid)
{
char sql[MAX_STRING_LEN];
snprintf(sql,sizeof(sql)-1,"delete from history where itemid=%d limit 500", itemid);
DBexecute(sql);
return DBaffected_rows();
}
void DBupdate_triggers_status_after_restart(void)
{
char sql[MAX_STRING_LEN];
int lastchange;
int now;
DB_RESULT result;
DB_RESULT result2;
DB_ROW row;
DB_ROW row2;
DB_TRIGGER trigger;
zabbix_log(LOG_LEVEL_DEBUG,"In DBupdate_triggers_after_restart()");
now=time(NULL);
snprintf(sql,sizeof(sql)-1,"select distinct t.triggerid,t.value from hosts h,items i,triggers t,functions f where f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid and i.nextcheck+i.delay<%d and i.key_<>'%s' and h.status not in (%d,%d)",now,SERVER_STATUS_KEY, HOST_STATUS_DELETED, HOST_STATUS_TEMPLATE);
zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql);
result = DBselect(sql);
while((row=DBfetch(result)))
{
trigger.triggerid=atoi(row[0]);
trigger.value=atoi(row[1]);
snprintf(sql,sizeof(sql)-1,"select min(i.nextcheck+i.delay) from hosts h,items i,triggers t,functions f where f.triggerid=t.triggerid and f.itemid=i.itemid and h.hostid=i.hostid and i.nextcheck<>0 and t.triggerid=%d and i.type<>%d",trigger.triggerid,ITEM_TYPE_TRAPPER);
zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql);
result2 = DBselect(sql);
row2=DBfetch(result2);
if(!row)
{
zabbix_log(LOG_LEVEL_DEBUG, "No triggers to update (2)");
DBfree_result(result2);
continue;
}
lastchange=atoi(row2[0]);
DBfree_result(result2);
DBupdate_trigger_value(&trigger,TRIGGER_VALUE_UNKNOWN,lastchange,"ZABBIX was down.");
}
DBfree_result(result);
zabbix_log(LOG_LEVEL_DEBUG,"End of DBupdate_triggers_after_restart()");
return;
}
void DBupdate_host_availability(int hostid,int available,int clock, char *error)
{
DB_RESULT result;
DB_ROW row;
char sql[MAX_STRING_LEN];
char error_esc[MAX_STRING_LEN];
int disable_until;
zabbix_log(LOG_LEVEL_DEBUG,"In update_host_availability()");
if(error!=NULL)
{
DBescape_string(error,error_esc,MAX_STRING_LEN);
}
else
{
strscpy(error_esc,"");
}
snprintf(sql,sizeof(sql)-1,"select available,disable_until from hosts where hostid=%d",hostid);
zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql);
result = DBselect(sql);
row=DBfetch(result);
if(!row)
{
zabbix_log(LOG_LEVEL_ERR, "Cannot select host with hostid [%d]",hostid);
zabbix_syslog("Cannot select host with hostid [%d]",hostid);
DBfree_result(result);
return;
}
disable_until = atoi(row[1]);
if(available == atoi(row[0]))
{
/* if((available==HOST_AVAILABLE_FALSE)
&&(clock+CONFIG_UNREACHABLE_PERIOD>disable_until) )
{
}
else
{*/
zabbix_log(LOG_LEVEL_DEBUG, "Host already has availability [%d]",available);
DBfree_result(result);
return;
/* }*/
}
DBfree_result(result);
if(available==HOST_AVAILABLE_TRUE)
{
snprintf(sql,sizeof(sql)-1,"update hosts set available=%d,error='',errors_from=0 where hostid=%d",HOST_AVAILABLE_TRUE,hostid);
zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql);
DBexecute(sql);
}
else if(available==HOST_AVAILABLE_FALSE)
{
/* if(disable_until+CONFIG_UNREACHABLE_PERIOD>clock)
{
snprintf(sql,sizeof(sql)-1,"update hosts set available=%d,disable_until=disable_until+%d,error='%s' where hostid=%d",HOST_AVAILABLE_FALSE,CONFIG_UNREACHABLE_DELAY,error_esc,hostid);
}
else
{
snprintf(sql,sizeof(sql)-1,"update hosts set available=%d,disable_until=%d,error='%s' where hostid=%d",HOST_AVAILABLE_FALSE,clock+CONFIG_UNREACHABLE_DELAY,error_esc,hostid);
}*/
snprintf(sql,sizeof(sql)-1,"update hosts set available=%d,error='%s' where hostid=%d",HOST_AVAILABLE_FALSE,error_esc,hostid);
zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql);
DBexecute(sql);
}
else
{
zabbix_log( LOG_LEVEL_ERR, "Unknown host availability [%d] for hostid [%d]", available, hostid);
zabbix_syslog("Unknown host availability [%d] for hostid [%d]", available, hostid);
return;
}
update_triggers_status_to_unknown(hostid,clock,"Host is unavailable.");
zabbix_log(LOG_LEVEL_DEBUG,"End of update_host_availability()");
return;
}
int DBupdate_item_status_to_notsupported(int itemid, char *error)
{
char sql[MAX_STRING_LEN];
char error_esc[MAX_STRING_LEN];
zabbix_log(LOG_LEVEL_DEBUG,"In DBupdate_item_status_to_notsupported()");
if(error!=NULL)
{
DBescape_string(error,error_esc,MAX_STRING_LEN);
}
else
{
strscpy(error_esc,"");
}
snprintf(sql,sizeof(sql)-1,"update items set status=%d,error='%s' where itemid=%d",ITEM_STATUS_NOTSUPPORTED,error_esc,itemid);
zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql);
DBexecute(sql);
return SUCCEED;
}
int DBadd_trend(int itemid, double value, int clock)
{
DB_RESULT result;
DB_ROW row;
char sql[MAX_STRING_LEN];
int hour;
int num;
double value_min, value_avg, value_max;
zabbix_log(LOG_LEVEL_DEBUG,"In add_trend()");
hour=clock-clock%3600;
snprintf(sql,sizeof(sql)-1,"select num,value_min,value_avg,value_max from trends where itemid=%d and clock=%d", itemid, hour);
zabbix_log(LOG_LEVEL_DEBUG,"SQL [%s]",sql);
result = DBselect(sql);
row=DBfetch(result);
if(row)
{
num=atoi(row[0]);
value_min=atof(row[1]);
value_avg=atof(row[2]);
value_max=atof(row[3]);
if(value<value_min) value_min=value;
/* Unfortunate mistake... */
/* if(value>value_avg) value_max=value;*/
if(value>value_max) value_max=value;
value_avg=(num*value_avg+value)/(num+1);
num++;
snprintf(sql,sizeof(sql)-1,"update trends set num=%d, value_min=%f, value_avg=%f, value_max=%f where itemid=%d and clock=%d", num, value_min, value_avg, value_max, itemid, hour);
}
else
{
snprintf(sql,sizeof(sql)-1,"insert into trends (clock,itemid,num,value_min,value_avg,value_max) values (%d,%d,%d,%f,%f,%f)", hour, itemid, 1, value, value, value);
}
DBexecute(sql);
DBfree_result(result);
return SUCCEED;
}
int DBadd_history(int itemid, double value, int clock)
{
char sql[MAX_STRING_LEN];
zabbix_log(LOG_LEVEL_DEBUG,"In add_history()");
snprintf(sql,sizeof(sql)-1,"insert into history (clock,itemid,value) values (%d,%d,%f)",clock,itemid,value);
DBexecute(sql);
DBadd_trend(itemid, value, clock);
return SUCCEED;
}
int DBadd_history_uint(int itemid, zbx_uint64_t value, int clock)
{
char sql[MAX_STRING_LEN];
zabbix_log(LOG_LEVEL_DEBUG,"In add_history_uint()");
snprintf(sql,sizeof(sql)-1,"insert into history_uint (clock,itemid,value) values (%d,%d," ZBX_FS_UI64 ")",clock,itemid,value);
DBexecute(sql);
DBadd_trend(itemid, (double)value, clock);
return SUCCEED;
}
int DBadd_history_str(int itemid, char *value, int clock)
{
char sql[MAX_STRING_LEN];
char value_esc[MAX_STRING_LEN];
zabbix_log(LOG_LEVEL_DEBUG,"In add_history_str()");
DBescape_string(value,value_esc,MAX_STRING_LEN);
snprintf(sql,sizeof(sql)-1,"insert into history_str (clock,itemid,value) values (%d,%d,'%s')",clock,itemid,value_esc);
DBexecute(sql);
return SUCCEED;
}
int DBadd_history_log(int itemid, char *value, int clock, int timestamp,char *source, int severity)
{
char sql[MAX_STRING_LEN];
char value_esc[MAX_STRING_LEN];
char source_esc[MAX_STRING_LEN];
zabbix_log(LOG_LEVEL_DEBUG,"In add_history_log()");
DBescape_string(value,value_esc,MAX_STRING_LEN);
DBescape_string(source,source_esc,MAX_STRING_LEN);
snprintf(sql,sizeof(sql)-1,"insert into history_log (clock,itemid,timestamp,value,source,severity) values (%d,%d,%d,'%s','%s',%d)",clock,itemid,timestamp,value_esc,source_esc,severity);
DBexecute(sql);
return SUCCEED;
}
int DBget_items_count(void)
{
int res;
char sql[MAX_STRING_LEN];
DB_RESULT result;
DB_ROW row;
zabbix_log(LOG_LEVEL_DEBUG,"In DBget_items_count()");
snprintf(sql,sizeof(sql)-1,"select count(*) from items");
result=DBselect(sql);
row=DBfetch(result);
if(!row)
{
zabbix_log(LOG_LEVEL_ERR, "Cannot execute query [%s]", sql);
zabbix_syslog("Cannot execute query [%s]", sql);
DBfree_result(result);
return 0;
}
res = atoi(row[0]);
DBfree_result(result);
return res;
}
int DBget_triggers_count(void)
{
int res;
char sql[MAX_STRING_LEN];
DB_RESULT result;
DB_ROW row;
zabbix_log(LOG_LEVEL_DEBUG,"In DBget_triggers_count()");
snprintf(sql,sizeof(sql)-1,"select count(*) from triggers");
result=DBselect(sql);
row=DBfetch(result);
if(!row)
{
zabbix_log(LOG_LEVEL_ERR, "Cannot execute query [%s]", sql);
zabbix_syslog("Cannot execute query [%s]", sql);
DBfree_result(result);
return 0;
}
res = atoi(row[0]);
DBfree_result(result);
return res;
}
int DBget_items_unsupported_count(void)
{
int res;
char sql[MAX_STRING_LEN];
DB_RESULT result;
DB_ROW row;
zabbix_log(LOG_LEVEL_DEBUG,"In DBget_items_unsupported_count()");
snprintf(sql,sizeof(sql)-1,"select count(*) from items where status=%d", ITEM_STATUS_NOTSUPPORTED);
result=DBselect(sql);
row=DBfetch(result);
if(!row)
{
zabbix_log(LOG_LEVEL_ERR, "Cannot execute query [%s]", sql);
zabbix_syslog("Cannot execute query [%s]", sql);
DBfree_result(result);
return 0;
}
res = atoi(row[0]);
DBfree_result(result);
return res;
}
int DBget_history_str_count(void)
{
int res;
char sql[MAX_STRING_LEN];
DB_RESULT result;
DB_ROW row;
zabbix_log(LOG_LEVEL_DEBUG,"In DBget_history_str_count()");
snprintf(sql,sizeof(sql)-1,"select count(*) from history_str");
result=DBselect(sql);
row=DBfetch(result);
if(!row)
{
zabbix_log(LOG_LEVEL_ERR, "Cannot execute query [%s]", sql);
zabbix_syslog("Cannot execute query [%s]", sql);
DBfree_result(result);
return 0;
}
res = atoi(row[0]);
DBfree_result(result);
return res;
}
int DBget_history_count(void)
{
int res;
char sql[MAX_STRING_LEN];
DB_RESULT result;
DB_ROW row;
zabbix_log(LOG_LEVEL_DEBUG,"In DBget_history_count()");
snprintf(sql,sizeof(sql)-1,"select count(*) from history");
result=DBselect(sql);
row=DBfetch(result);
if(!row)
{
zabbix_log(LOG_LEVEL_ERR, "Cannot execute query [%s]", sql);
zabbix_syslog("Cannot execute query [%s]", sql);
DBfree_result(result);
return 0;
}
res = atoi(row[0]);
DBfree_result(result);
return res;
}
int DBget_trends_count(void)
{
int res;
char sql[MAX_STRING_LEN];
DB_RESULT result;
DB_ROW row;
zabbix_log(LOG_LEVEL_DEBUG,"In DBget_trends_count()");
snprintf(sql,sizeof(sql)-1,"select count(*) from trends");
result=DBselect(sql);
row=DBfetch(result);
if(!row)
{
zabbix_log(LOG_LEVEL_ERR, "Cannot execute query [%s]", sql);
zabbix_syslog("Cannot execute query [%s]", sql);
DBfree_result(result);
return 0;
}
res = atoi(row[0]);
DBfree_result(result);
return res;
}
int DBget_queue_count(void)
{
int res;
char sql[MAX_STRING_LEN];
DB_RESULT result;
DB_ROW row;
int now;
zabbix_log(LOG_LEVEL_DEBUG,"In DBget_queue_count()");
now=time(NULL);
/* snprintf(sql,sizeof(sql)-1,"select count(*) from items i,hosts h where i.status=%d and i.type not in (%d) and h.status=%d and i.hostid=h.hostid and i.nextcheck<%d and i.key_<>'status'", ITEM_STATUS_ACTIVE, ITEM_TYPE_TRAPPER, HOST_STATUS_MONITORED, now);*/
snprintf(sql,sizeof(sql)-1,"select count(*) from items i,hosts h where i.status=%d and i.type not in (%d) and ((h.status=%d and h.available!=%d) or (h.status=%d and h.available=%d and h.disable_until<=%d)) and i.hostid=h.hostid and i.nextcheck<%d and i.key_ not in ('%s','%s','%s','%s')", ITEM_STATUS_ACTIVE, ITEM_TYPE_TRAPPER, HOST_STATUS_MONITORED, HOST_AVAILABLE_FALSE, HOST_STATUS_MONITORED, HOST_AVAILABLE_FALSE, now, now, SERVER_STATUS_KEY, SERVER_ICMPPING_KEY, SERVER_ICMPPINGSEC_KEY, SERVER_ZABBIXLOG_KEY);
result=DBselect(sql);
row=DBfetch(result);
if(!row)
{
zabbix_log(LOG_LEVEL_ERR, "Cannot execute query [%s]", sql);
zabbix_syslog("Cannot execute query [%s]", sql);
DBfree_result(result);
return 0;
}
res = atoi(row[0]);
DBfree_result(result);
return res;
}
int DBadd_alert(int actionid, int userid, int triggerid, int mediatypeid, char *sendto, char *subject, char *message, int maxrepeats, int repeatdelay)
{
int now;
char sql[MAX_STRING_LEN];
char sendto_esc[MAX_STRING_LEN];
char subject_esc[MAX_STRING_LEN];
char message_esc[MAX_STRING_LEN];
zabbix_log(LOG_LEVEL_DEBUG,"In add_alert()");
now = time(NULL);
/* Does not work on PostgreSQL */
/* snprintf(sql,sizeof(sql)-1,"insert into alerts (alertid,actionid,clock,mediatypeid,sendto,subject,message,status,retries) values (NULL,%d,%d,%d,'%s','%s','%s',0,0)",actionid,now,mediatypeid,sendto,subject,message);*/
DBescape_string(sendto,sendto_esc,MAX_STRING_LEN);
DBescape_string(subject,subject_esc,MAX_STRING_LEN);
DBescape_string(message,message_esc,MAX_STRING_LEN);
snprintf(sql,sizeof(sql)-1,"insert into alerts (actionid,triggerid,userid,clock,mediatypeid,sendto,subject,message,status,retries,maxrepeats,delay) values (%d,%d,%d,%d,%d,'%s','%s','%s',0,0,%d,%d)",actionid,triggerid,userid,now,mediatypeid,sendto_esc,subject_esc,message_esc, maxrepeats, repeatdelay);
DBexecute(sql);
return SUCCEED;
}
void DBvacuum(void)
{
#ifdef HAVE_PGSQL
char *table_for_housekeeping[]={"services", "services_links", "graphs_items", "graphs", "sysmaps_links",
"sysmaps_hosts", "sysmaps", "config", "groups", "hosts_groups", "alerts",
"actions", "alarms", "functions", "history", "history_str", "hosts", "trends",
"items", "media", "media_type", "triggers", "trigger_depends", "users",
"sessions", "rights", "service_alarms", "profiles", "screens", "screens_items",
"stats",
NULL};
char sql[MAX_STRING_LEN];
char *table;
int i;
#ifdef HAVE_FUNCTION_SETPROCTITLE
setproctitle("housekeeper [vacuum DB]");
#endif
i=0;
while (NULL != (table = table_for_housekeeping[i++]))
{
snprintf(sql,sizeof(sql)-1,"vacuum analyze %s", table);
DBexecute(sql);
}
#endif
#ifdef HAVE_MYSQL
/* Nothing to do */
#endif
}
/* Broken:
void DBescape_string(char *from, char *to, int maxlen)
{
int i,ptr;
char *f;
ptr=0;
f=(char *)strdup(from);
for(i=0;f[i]!=0;i++)
{
if( (f[i]=='\'') || (f[i]=='\\'))
{
if(ptr>maxlen-1) break;
to[ptr]='\\';
if(ptr+1>maxlen-1) break;
to[ptr+1]=f[i];
ptr+=2;
}
else
{
if(ptr>maxlen-1) break;
to[ptr]=f[i];
ptr++;
}
}
free(f);
to[ptr]=0;
to[maxlen-1]=0;
}
*/
void DBescape_string(char *from, char *to, int maxlen)
{
int i,ptr;
maxlen--;
for(i=0, ptr=0; from && from[i] && ptr < maxlen; i++)
{
if( (from[i] == '\'') || (from[i] == '\\'))
{
to[ptr++] = '\\';
if(ptr >= maxlen) break;
}
to[ptr++] = from[i];
}
to[ptr]=0;
}
void DBget_item_from_db(DB_ITEM *item,DB_ROW row)
{
char *s;
item->itemid=atoi(row[0]);
strscpy(item->key,row[1]);
item->host=row[2];
item->port=atoi(row[3]);
item->delay=atoi(row[4]);
item->description=row[5];
item->nextcheck=atoi(row[6]);
item->type=atoi(row[7]);
item->snmp_community=row[8];
item->snmp_oid=row[9];
item->useip=atoi(row[10]);
item->ip=row[11];
item->history=atoi(row[12]);
s=row[13];
if(DBis_null(s)==SUCCEED)
{
item->lastvalue_null=1;
}
else
{
item->lastvalue_null=0;
item->lastvalue_str=s;
item->lastvalue=atof(s);
}
s=row[14];
if(DBis_null(s)==SUCCEED)
{
item->prevvalue_null=1;
}
else
{
item->prevvalue_null=0;
item->prevvalue_str=s;
item->prevvalue=atof(s);
}
item->hostid=atoi(row[15]);
item->host_status=atoi(row[16]);
item->value_type=atoi(row[17]);
item->host_errors_from=atoi(row[18]);
item->snmp_port=atoi(row[19]);
item->delta=atoi(row[20]);
s=row[21];
if(DBis_null(s)==SUCCEED)
{
item->prevorgvalue_null=1;
}
else
{
item->prevorgvalue_null=0;
item->prevorgvalue=atof(s);
}
s=row[22];
if(DBis_null(s)==SUCCEED)
{
item->lastclock=0;
}
else
{
item->lastclock=atoi(s);
}
item->units=row[23];
item->multiplier=atoi(row[24]);
item->snmpv3_securityname = row[25];
item->snmpv3_securitylevel = atoi(row[26]);
item->snmpv3_authpassphrase = row[27];
item->snmpv3_privpassphrase = row[28];
item->formula = row[29];
item->host_available=atoi(row[30]);
item->status=atoi(row[31]);
item->trapper_hosts=row[32];
item->logtimefmt=row[33];
item->valuemapid=atoi(row[34]);
}
int DBget_default_escalation_id()
{
DB_RESULT dbresult;
DB_ROW row;
int res = SUCCEED;
char sql[MAX_STRING_LEN];
/* 0 is added to distinguish between lastvalue==NULL and empty result */
snprintf( sql, sizeof(sql)-1, "select escalationid from escalations where dflt=1");
dbresult = DBselect(sql);
row = DBfetch(dbresult);
if(!row)
{
zabbix_log(LOG_LEVEL_WARNING, "No default escalation defined");
zabbix_syslog("No default escalation defined");
res = FAIL;
}
res = atoi(row[0]);
DBfree_result(dbresult);
return res;
}
|