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
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
|
/*
** Zabbix
** Copyright (C) 2001-2019 Zabbix SIA
**
** 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include "common.h"
#include "log.h"
#include "setproctitle.h"
/* scheduler support */
#define ZBX_SCHEDULER_FILTER_DAY 1
#define ZBX_SCHEDULER_FILTER_HOUR 2
#define ZBX_SCHEDULER_FILTER_MINUTE 3
#define ZBX_SCHEDULER_FILTER_SECOND 4
typedef struct
{
int start_day; /* day of week when period starts */
int end_day; /* day of week when period ends, included */
int start_time; /* number of seconds from the beginning of the day when period starts */
int end_time; /* number of seconds from the beginning of the day when period ends, not included */
}
zbx_time_period_t;
typedef struct zbx_flexible_interval
{
zbx_time_period_t period;
int delay;
struct zbx_flexible_interval *next;
}
zbx_flexible_interval_t;
typedef struct zbx_scheduler_filter
{
int start;
int end;
int step;
struct zbx_scheduler_filter *next;
}
zbx_scheduler_filter_t;
typedef struct zbx_scheduler_interval
{
zbx_scheduler_filter_t *mdays;
zbx_scheduler_filter_t *wdays;
zbx_scheduler_filter_t *hours;
zbx_scheduler_filter_t *minutes;
zbx_scheduler_filter_t *seconds;
int filter_level;
struct zbx_scheduler_interval *next;
}
zbx_scheduler_interval_t;
struct zbx_custom_interval
{
zbx_flexible_interval_t *flexible;
zbx_scheduler_interval_t *scheduling;
};
static ZBX_THREAD_LOCAL volatile sig_atomic_t zbx_timed_out; /* 0 - no timeout occurred, 1 - SIGALRM took place */
#ifdef _WINDOWS
char ZABBIX_SERVICE_NAME[ZBX_SERVICE_NAME_LEN] = APPLICATION_NAME;
char ZABBIX_EVENT_SOURCE[ZBX_SERVICE_NAME_LEN] = APPLICATION_NAME;
int __zbx_stat(const char *path, zbx_stat_t *buf)
{
int ret, fd;
wchar_t *wpath;
wpath = zbx_utf8_to_unicode(path);
if (-1 == (ret = _wstat64(wpath, buf)))
goto out;
if (0 != S_ISDIR(buf->st_mode) || 0 != buf->st_size)
goto out;
/* In the case of symlinks _wstat64 returns zero file size. */
/* Try to work around it by opening the file and using fstat. */
ret = -1;
if (-1 != (fd = _wopen(wpath, O_RDONLY)))
{
ret = _fstat64(fd, buf);
_close(fd);
}
out:
zbx_free(wpath);
return ret;
}
#endif
/******************************************************************************
* *
* Function: get_program_name *
* *
* Purpose: return program name without path *
* *
* Parameters: path *
* *
* Return value: program name without path *
* *
* Author: Eugene Grigorjev *
* *
******************************************************************************/
const char *get_program_name(const char *path)
{
const char *filename = NULL;
for (filename = path; path && *path; path++)
{
if ('\\' == *path || '/' == *path)
filename = path + 1;
}
return filename;
}
/******************************************************************************
* *
* Function: zbx_timespec *
* *
* Purpose: Gets the current time. *
* *
* Author: Alexander Vladishev *
* *
* Comments: Time in seconds since midnight (00:00:00), *
* January 1, 1970, coordinated universal time (UTC). *
* *
******************************************************************************/
void zbx_timespec(zbx_timespec_t *ts)
{
static ZBX_THREAD_LOCAL zbx_timespec_t last_ts = {0, 0};
static ZBX_THREAD_LOCAL int corr = 0;
#ifdef _WINDOWS
static ZBX_THREAD_LOCAL LARGE_INTEGER tickPerSecond = {0};
struct _timeb tb;
#else
struct timeval tv;
int rc = -1;
# ifdef HAVE_TIME_CLOCK_GETTIME
struct timespec tp;
# endif
#endif
#ifdef _WINDOWS
if (0 == tickPerSecond.QuadPart)
QueryPerformanceFrequency(&tickPerSecond);
_ftime(&tb);
ts->sec = (int)tb.time;
ts->ns = tb.millitm * 1000000;
if (0 != tickPerSecond.QuadPart)
{
LARGE_INTEGER tick;
if (TRUE == QueryPerformanceCounter(&tick))
{
static ZBX_THREAD_LOCAL LARGE_INTEGER last_tick = {0};
if (0 < last_tick.QuadPart)
{
LARGE_INTEGER qpc_tick = {0}, ntp_tick = {0};
/* _ftime () returns precision in milliseconds, but 'ns' could be increased up to 1ms */
if (last_ts.sec == ts->sec && last_ts.ns > ts->ns && 1000000 > (last_ts.ns - ts->ns))
{
ts->ns = last_ts.ns;
}
else
{
ntp_tick.QuadPart = tickPerSecond.QuadPart * (ts->sec - last_ts.sec) +
tickPerSecond.QuadPart * (ts->ns - last_ts.ns) / 1000000000;
}
/* host system time can shift backwards, then correction is not reasonable */
if (0 <= ntp_tick.QuadPart)
qpc_tick.QuadPart = tick.QuadPart - last_tick.QuadPart - ntp_tick.QuadPart;
if (0 < qpc_tick.QuadPart && qpc_tick.QuadPart < tickPerSecond.QuadPart)
{
int ns = (int)(1000000000 * qpc_tick.QuadPart / tickPerSecond.QuadPart);
if (1000000 > ns) /* value less than 1 millisecond */
{
ts->ns += ns;
while (ts->ns >= 1000000000)
{
ts->sec++;
ts->ns -= 1000000000;
}
}
}
}
last_tick = tick;
}
}
#else /* not _WINDOWS */
#ifdef HAVE_TIME_CLOCK_GETTIME
if (0 == (rc = clock_gettime(CLOCK_REALTIME, &tp)))
{
ts->sec = (int)tp.tv_sec;
ts->ns = (int)tp.tv_nsec;
}
#endif /* HAVE_TIME_CLOCK_GETTIME */
if (0 != rc && 0 == (rc = gettimeofday(&tv, NULL)))
{
ts->sec = (int)tv.tv_sec;
ts->ns = (int)tv.tv_usec * 1000;
}
if (0 != rc)
{
ts->sec = (int)time(NULL);
ts->ns = 0;
}
#endif /* not _WINDOWS */
if (last_ts.ns == ts->ns && last_ts.sec == ts->sec)
{
ts->ns += ++corr;
while (ts->ns >= 1000000000)
{
ts->sec++;
ts->ns -= 1000000000;
}
}
else
{
last_ts.sec = ts->sec;
last_ts.ns = ts->ns;
corr = 0;
}
}
/******************************************************************************
* *
* Function: zbx_time *
* *
* Purpose: Gets the current time. *
* *
* Return value: Time in seconds *
* *
* Author: Eugene Grigorjev *
* *
* Comments: Time in seconds since midnight (00:00:00), *
* January 1, 1970, coordinated universal time (UTC). *
* *
******************************************************************************/
double zbx_time(void)
{
zbx_timespec_t ts;
zbx_timespec(&ts);
return (double)ts.sec + 1.0e-9 * (double)ts.ns;
}
/******************************************************************************
* *
* Function: zbx_current_time *
* *
* Purpose: Gets the current time including UTC offset *
* *
* Return value: Time in seconds *
* *
* Author: Eugene Grigorjev *
* *
******************************************************************************/
double zbx_current_time(void)
{
return zbx_time() + ZBX_JAN_1970_IN_SEC;
}
/******************************************************************************
* *
* Function: is_leap_year *
* *
* Return value: SUCCEED - year is a leap year *
* FAIL - year is not a leap year *
* *
******************************************************************************/
static int is_leap_year(int year)
{
return 0 == year % 4 && (0 != year % 100 || 0 == year % 400) ? SUCCEED : FAIL;
}
/******************************************************************************
* *
* Function: zbx_get_time *
* *
* Purpose: *
* get current time and store it in memory locations provided by caller *
* *
* Parameters: *
* tm - [OUT] broken-down representation of the current time *
* milliseconds - [OUT] milliseconds since the previous second *
* tz - [OUT] local time offset from UTC (optional) *
* *
* Comments: *
* On Windows localtime() and gmtime() return pointers to static, *
* thread-local storage locations. On Unix localtime() and gmtime() are *
* not thread-safe and re-entrant as they return pointers to static *
* storage locations which can be overwritten by localtime(), gmtime() *
* or other time functions in other threads or signal handlers. To avoid *
* this we use localtime_r() and gmtime_r(). *
* *
******************************************************************************/
void zbx_get_time(struct tm *tm, long *milliseconds, zbx_timezone_t *tz)
{
#ifdef _WINDOWS
struct _timeb current_time;
_ftime(¤t_time);
*tm = *localtime(¤t_time.time); /* localtime() cannot return NULL if called with valid parameter */
*milliseconds = current_time.millitm;
#else
struct timeval current_time;
gettimeofday(¤t_time, NULL);
localtime_r(¤t_time.tv_sec, tm);
*milliseconds = current_time.tv_usec / 1000;
#endif
if (NULL != tz)
{
#ifdef HAVE_TM_TM_GMTOFF
# define ZBX_UTC_OFF tm->tm_gmtoff
#else
# define ZBX_UTC_OFF offset
long offset;
struct tm tm_utc;
#ifdef _WINDOWS
tm_utc = *gmtime(¤t_time.time); /* gmtime() cannot return NULL if called with valid parameter */
#else
gmtime_r(¤t_time.tv_sec, &tm_utc);
#endif
offset = (tm->tm_yday - tm_utc.tm_yday) * SEC_PER_DAY + (tm->tm_hour - tm_utc.tm_hour) * SEC_PER_HOUR +
(tm->tm_min - tm_utc.tm_min) * SEC_PER_MIN; /* assuming seconds are equal */
while (tm->tm_year > tm_utc.tm_year)
offset += (SUCCEED == is_leap_year(tm_utc.tm_year++) ? SEC_PER_YEAR + SEC_PER_DAY : SEC_PER_YEAR);
while (tm->tm_year < tm_utc.tm_year)
offset -= (SUCCEED == is_leap_year(--tm_utc.tm_year) ? SEC_PER_YEAR + SEC_PER_DAY : SEC_PER_YEAR);
#endif
tz->tz_sign = (0 <= ZBX_UTC_OFF ? '+' : '-');
tz->tz_hour = labs(ZBX_UTC_OFF) / SEC_PER_HOUR;
tz->tz_min = (labs(ZBX_UTC_OFF) - tz->tz_hour * SEC_PER_HOUR) / SEC_PER_MIN;
/* assuming no remaining seconds like in historic Asia/Riyadh87, Asia/Riyadh88 and Asia/Riyadh89 */
#undef ZBX_UTC_OFF
}
}
/******************************************************************************
* *
* Function: zbx_utc_time *
* *
* Purpose: get UTC time from time from broken down time elements *
* *
* Parameters: *
* year - [IN] year (1970-...) *
* month - [IN] month (1-12) *
* mday - [IN] day of month (1-..., depending on month and year) *
* hour - [IN] hours (0-23) *
* min - [IN] minutes (0-59) *
* sec - [IN] seconds (0-61, leap seconds are not strictly validated) *
* t - [OUT] Epoch timestamp *
* *
* Return value: SUCCEED - date is valid and resulting timestamp is positive *
* FAIL - otherwise *
* *
******************************************************************************/
int zbx_utc_time(int year, int mon, int mday, int hour, int min, int sec, int *t)
{
/* number of leap years before but not including year */
#define ZBX_LEAP_YEARS(year) (((year) - 1) / 4 - ((year) - 1) / 100 + ((year) - 1) / 400)
/* days since the beginning of non-leap year till the beginning of the month */
static const int month_day[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
static const int epoch_year = 1970;
if (epoch_year <= year && 1 <= mon && mon <= 12 && 1 <= mday && mday <= zbx_day_in_month(year, mon) &&
0 <= hour && hour <= 23 && 0 <= min && min <= 59 && 0 <= sec && sec <= 61 &&
0 <= (*t = (year - epoch_year) * SEC_PER_YEAR +
(ZBX_LEAP_YEARS(2 < mon ? year + 1 : year) - ZBX_LEAP_YEARS(epoch_year)) * SEC_PER_DAY +
(month_day[mon - 1] + mday - 1) * SEC_PER_DAY + hour * SEC_PER_HOUR + min * SEC_PER_MIN + sec))
{
return SUCCEED;
}
return FAIL;
#undef ZBX_LEAP_YEARS
}
/******************************************************************************
* *
* Function: zbx_day_in_month *
* *
* Purpose: returns number of days in a month *
* *
* Parameters: *
* year - [IN] year *
* mon - [IN] month (1-12) *
* *
* Return value: 28-31 depending on number of days in the month, defaults to *
* 30 if the month is outside of allowed range *
* *
* Author: Alexander Vladishev *
* *
******************************************************************************/
int zbx_day_in_month(int year, int mon)
{
/* number of days in the month of a non-leap year */
static const unsigned char month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (1 <= mon && mon <= 12) /* add one day in February of a leap year */
return month[mon - 1] + (2 == mon && SUCCEED == is_leap_year(year) ? 1 : 0);
return 30;
}
/******************************************************************************
* *
* Function: zbx_calloc2 *
* *
* Purpose: allocates nmemb * size bytes of memory and fills it with zeros *
* *
* Return value: returns a pointer to the newly allocated memory *
* *
* Author: Eugene Grigorjev, Rudolfs Kreicbergs *
* *
******************************************************************************/
void *zbx_calloc2(const char *filename, int line, void *old, size_t nmemb, size_t size)
{
int max_attempts;
void *ptr = NULL;
/* old pointer must be NULL */
if (NULL != old)
{
zabbix_log(LOG_LEVEL_CRIT, "[file:%s,line:%d] zbx_calloc: allocating already allocated memory. "
"Please report this to Zabbix developers.",
filename, line);
}
for (
max_attempts = 10, nmemb = MAX(nmemb, 1), size = MAX(size, 1);
0 < max_attempts && NULL == ptr;
ptr = calloc(nmemb, size), max_attempts--
);
if (NULL != ptr)
return ptr;
zabbix_log(LOG_LEVEL_CRIT, "[file:%s,line:%d] zbx_calloc: out of memory. Requested " ZBX_FS_SIZE_T " bytes.",
filename, line, (zbx_fs_size_t)size);
exit(EXIT_FAILURE);
}
/******************************************************************************
* *
* Function: zbx_malloc2 *
* *
* Purpose: allocates size bytes of memory *
* *
* Return value: returns a pointer to the newly allocated memory *
* *
* Author: Eugene Grigorjev *
* *
******************************************************************************/
void *zbx_malloc2(const char *filename, int line, void *old, size_t size)
{
int max_attempts;
void *ptr = NULL;
/* old pointer must be NULL */
if (NULL != old)
{
zabbix_log(LOG_LEVEL_CRIT, "[file:%s,line:%d] zbx_malloc: allocating already allocated memory. "
"Please report this to Zabbix developers.",
filename, line);
}
for (
max_attempts = 10, size = MAX(size, 1);
0 < max_attempts && NULL == ptr;
ptr = malloc(size), max_attempts--
);
if (NULL != ptr)
return ptr;
zabbix_log(LOG_LEVEL_CRIT, "[file:%s,line:%d] zbx_malloc: out of memory. Requested " ZBX_FS_SIZE_T " bytes.",
filename, line, (zbx_fs_size_t)size);
exit(EXIT_FAILURE);
}
/******************************************************************************
* *
* Function: zbx_realloc2 *
* *
* Purpose: changes the size of the memory block pointed to by old *
* to size bytes *
* *
* Return value: returns a pointer to the newly allocated memory *
* *
* Author: Eugene Grigorjev *
* *
******************************************************************************/
void *zbx_realloc2(const char *filename, int line, void *old, size_t size)
{
int max_attempts;
void *ptr = NULL;
for (
max_attempts = 10, size = MAX(size, 1);
0 < max_attempts && NULL == ptr;
ptr = realloc(old, size), max_attempts--
);
if (NULL != ptr)
return ptr;
zabbix_log(LOG_LEVEL_CRIT, "[file:%s,line:%d] zbx_realloc: out of memory. Requested " ZBX_FS_SIZE_T " bytes.",
filename, line, (zbx_fs_size_t)size);
exit(EXIT_FAILURE);
}
char *zbx_strdup2(const char *filename, int line, char *old, const char *str)
{
int retry;
char *ptr = NULL;
zbx_free(old);
for (retry = 10; 0 < retry && NULL == ptr; ptr = strdup(str), retry--)
;
if (NULL != ptr)
return ptr;
zabbix_log(LOG_LEVEL_CRIT, "[file:%s,line:%d] zbx_strdup: out of memory. Requested " ZBX_FS_SIZE_T " bytes.",
filename, line, (zbx_fs_size_t)(strlen(str) + 1));
exit(EXIT_FAILURE);
}
/****************************************************************************************
* *
* Function: zbx_guaranteed_memset *
* *
* Purpose: For overwriting sensitive data in memory. *
* Similar to memset() but should not be optimized out by a compiler. *
* *
* Derived from: *
* http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/protect-secrets.html *
* See also: *
* http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1381.pdf on secure_memset() *
* *
****************************************************************************************/
void *zbx_guaranteed_memset(void *v, int c, size_t n)
{
volatile signed char *p = (volatile signed char *)v;
while (0 != n--)
*p++ = (signed char)c;
return v;
}
/******************************************************************************
* *
* Function: __zbx_zbx_setproctitle *
* *
* Purpose: set process title *
* *
* Author: Eugene Grigorjev *
* *
******************************************************************************/
void zbx_setproctitle(const char *fmt, ...)
{
#if defined(HAVE_FUNCTION_SETPROCTITLE) || defined(PS_OVERWRITE_ARGV) || defined(PS_PSTAT_ARGV)
char title[MAX_STRING_LEN];
va_list args;
va_start(args, fmt);
zbx_vsnprintf(title, sizeof(title), fmt, args);
va_end(args);
zabbix_log(LOG_LEVEL_DEBUG, "%s() title:'%s'", __func__, title);
#endif
#if defined(HAVE_FUNCTION_SETPROCTITLE)
setproctitle("%s", title);
#elif defined(PS_OVERWRITE_ARGV) || defined(PS_PSTAT_ARGV)
setproctitle_set_status(title);
#endif
}
/******************************************************************************
* *
* Function: check_time_period *
* *
* Purpose: check if current time is within given period *
* *
* Parameters: period - [IN] preprocessed time period *
* tm - [IN] broken-down time for comparison *
* *
* Return value: FAIL - out of period, SUCCEED - within the period *
* *
* Author: Alexei Vladishev *
* *
******************************************************************************/
static int check_time_period(const zbx_time_period_t period, struct tm *tm)
{
int day, time;
day = 0 == tm->tm_wday ? 7 : tm->tm_wday;
time = SEC_PER_HOUR * tm->tm_hour + SEC_PER_MIN * tm->tm_min + tm->tm_sec;
return period.start_day <= day && day <= period.end_day && period.start_time <= time && time < period.end_time ?
SUCCEED : FAIL;
}
/******************************************************************************
* *
* Function: get_current_delay *
* *
* Purpose: return delay value that is currently applicable *
* *
* Parameters: default_delay - [IN] default delay value, can be overridden *
* flex_intervals - [IN] preprocessed flexible intervals *
* now - [IN] current time *
* *
* Return value: delay value - either default or minimum delay value *
* out of all applicable intervals *
* *
* Author: Alexei Vladishev, Alexander Vladishev, Aleksandrs Saveljevs *
* *
******************************************************************************/
static int get_current_delay(int default_delay, const zbx_flexible_interval_t *flex_intervals, time_t now)
{
int current_delay = -1;
while (NULL != flex_intervals)
{
if ((-1 == current_delay || flex_intervals->delay < current_delay) &&
SUCCEED == check_time_period(flex_intervals->period, localtime(&now)))
{
current_delay = flex_intervals->delay;
}
flex_intervals = flex_intervals->next;
}
return -1 == current_delay ? default_delay : current_delay;
}
/******************************************************************************
* *
* Function: get_next_delay_interval *
* *
* Purpose: return time when next delay settings take effect *
* *
* Parameters: flex_intervals - [IN] preprocessed flexible intervals *
* now - [IN] current time *
* next_interval - [OUT] start of next delay interval *
* *
* Return value: SUCCEED - there is a next interval *
* FAIL - otherwise (in this case, next_interval is unaffected) *
* *
* Author: Alexei Vladishev, Alexander Vladishev, Aleksandrs Saveljevs *
* *
******************************************************************************/
static int get_next_delay_interval(const zbx_flexible_interval_t *flex_intervals, time_t now, time_t *next_interval)
{
int day, time, next = 0, candidate;
struct tm *tm;
if (NULL == flex_intervals)
return FAIL;
tm = localtime(&now);
day = 0 == tm->tm_wday ? 7 : tm->tm_wday;
time = SEC_PER_HOUR * tm->tm_hour + SEC_PER_MIN * tm->tm_min + tm->tm_sec;
for (; NULL != flex_intervals; flex_intervals = flex_intervals->next)
{
const zbx_time_period_t *p = &flex_intervals->period;
if (p->start_day <= day && day <= p->end_day && time < p->end_time) /* will be active today */
{
if (time < p->start_time) /* hasn't been active today yet */
candidate = p->start_time;
else /* currently active */
candidate = p->end_time;
}
else if (day < p->end_day) /* will be active this week */
{
if (day < p->start_day) /* hasn't been active this week yet */
candidate = SEC_PER_DAY * (p->start_day - day) + p->start_time;
else /* has been active this week and will be active at least once more by the end of it */
candidate = SEC_PER_DAY + p->start_time; /* therefore will be active tomorrow */
}
else /* will be active next week */
candidate = SEC_PER_DAY * (p->start_day + 7 - day) + p->start_time;
if (0 == next || next > candidate)
next = candidate;
}
if (0 == next)
return FAIL;
*next_interval = now - time + next;
return SUCCEED;
}
/******************************************************************************
* *
* Function: time_parse *
* *
* Purpose: parses time of day *
* *
* Parameters: time - [OUT] number of seconds since the beginning of *
* the day corresponding to a given time of day *
* text - [IN] text to parse *
* len - [IN] number of characters available for parsing *
* parsed_len - [OUT] number of characters recognized as time *
* *
* Return value: SUCCEED - text was successfully parsed as time of day *
* FAIL - otherwise (time and parsed_len remain untouched) *
* *
* Comments: !!! Don't forget to sync code with PHP !!! *
* Supported formats are hh:mm, h:mm and 0h:mm; 0 <= hours <= 24; *
* 0 <= minutes <= 59; if hours == 24 then minutes must be 0. *
* *
******************************************************************************/
static int time_parse(int *time, const char *text, int len, int *parsed_len)
{
const int old_len = len;
const char *ptr;
int hours, minutes;
for (ptr = text; 0 < len && 0 != isdigit(*ptr) && 2 >= ptr - text; len--, ptr++)
;
if (SUCCEED != is_uint_n_range(text, ptr - text, &hours, sizeof(hours), 0, 24))
return FAIL;
if (0 >= len-- || ':' != *ptr++)
return FAIL;
for (text = ptr; 0 < len && 0 != isdigit(*ptr) && 2 >= ptr - text; len--, ptr++)
;
if (2 != ptr - text)
return FAIL;
if (SUCCEED != is_uint_n_range(text, 2, &minutes, sizeof(minutes), 0, 59))
return FAIL;
if (24 == hours && 0 != minutes)
return FAIL;
*parsed_len = old_len - len;
*time = SEC_PER_HOUR * hours + SEC_PER_MIN * minutes;
return SUCCEED;
}
/******************************************************************************
* *
* Function: time_period_parse *
* *
* Purpose: parses time period *
* *
* Parameters: period - [OUT] time period structure *
* text - [IN] text to parse *
* len - [IN] number of characters available for parsing *
* *
* Return value: SUCCEED - text was successfully parsed as time period *
* FAIL - otherwise *
* *
* Comments: !!! Don't forget to sync code with PHP !!! *
* Supported format is d[-d],time-time where 1 <= d <= 7 *
* *
******************************************************************************/
static int time_period_parse(zbx_time_period_t *period, const char *text, int len)
{
int parsed_len;
if (0 >= len-- || '1' > *text || '7' < *text)
return FAIL;
period->start_day = *text++ - '0';
if (0 >= len)
return FAIL;
if ('-' == *text)
{
text++;
len--;
if (0 >= len-- || '1' > *text || '7' < *text)
return FAIL;
period->end_day = *text++ - '0';
if (period->start_day > period->end_day)
return FAIL;
}
else
period->end_day = period->start_day;
if (0 >= len-- || ',' != *text++)
return FAIL;
if (SUCCEED != time_parse(&period->start_time, text, len, &parsed_len))
return FAIL;
text += parsed_len;
len -= parsed_len;
if (0 >= len-- || '-' != *text++)
return FAIL;
if (SUCCEED != time_parse(&period->end_time, text, len, &parsed_len))
return FAIL;
if (period->start_time >= period->end_time)
return FAIL;
if (0 != (len -= parsed_len))
return FAIL;
return SUCCEED;
}
/******************************************************************************
* *
* Function: zbx_check_time_period *
* *
* Purpose: validate time period and check if specified time is within it *
* *
* Parameters: period - [IN] semicolon-separated list of time periods in one *
* of the following formats: *
* d1-d2,h1:m1-h2:m2 *
* or d1,h1:m1-h2:m2 *
* time - [IN] time to check *
* res - [OUT] check result: *
* SUCCEED - if time is within period *
* FAIL - otherwise *
* *
* Return value: validation result (SUCCEED - valid, FAIL - invalid) *
* *
* Comments: !!! Don't forget to sync code with PHP !!! *
* *
******************************************************************************/
int zbx_check_time_period(const char *period, time_t time, int *res)
{
int res_total = FAIL;
const char *next;
struct tm *tm;
zbx_time_period_t tp;
tm = localtime(&time);
next = strchr(period, ';');
while (SUCCEED == time_period_parse(&tp, period, (NULL == next ? (int)strlen(period) : (int)(next - period))))
{
if (SUCCEED == check_time_period(tp, tm))
res_total = SUCCEED; /* no short-circuits, validate all periods before return */
if (NULL == next)
{
*res = res_total;
return SUCCEED;
}
period = next + 1;
next = strchr(period, ';');
}
return FAIL;
}
/******************************************************************************
* *
* Function: flexible_interval_free *
* *
* Purpose: frees flexible interval *
* *
* Parameters: interval - [IN] flexible interval *
* *
******************************************************************************/
static void flexible_interval_free(zbx_flexible_interval_t *interval)
{
zbx_flexible_interval_t *interval_next;
for (; NULL != interval; interval = interval_next)
{
interval_next = interval->next;
zbx_free(interval);
}
}
/******************************************************************************
* *
* Function: flexible_interval_parse *
* *
* Purpose: parses flexible interval *
* *
* Parameters: interval - [IN/OUT] the first interval *
* text - [IN] the text to parse *
* len - [IN] the text length *
* *
* Return value: SUCCEED - the interval was successfully parsed *
* FAIL - otherwise *
* *
* Comments: !!! Don't forget to sync code with PHP !!! *
* Supported format is delay/period *
* *
******************************************************************************/
static int flexible_interval_parse(zbx_flexible_interval_t *interval, const char *text, int len)
{
const char *ptr;
for (ptr = text; 0 < len && '\0' != *ptr && '/' != *ptr; len--, ptr++)
;
if (SUCCEED != is_time_suffix(text, &interval->delay, (int)(ptr - text)))
return FAIL;
if (0 >= len-- || '/' != *ptr++)
return FAIL;
return time_period_parse(&interval->period, ptr, len);
}
/******************************************************************************
* *
* Function: calculate_dayofweek *
* *
* Purpose: calculates day of week *
* *
* Parameters: year - [IN] the year (>1752) *
* mon - [IN] the month (1-12) *
* mday - [IN] the month day (1-31) *
* *
* Return value: The day of week: 1 - Monday, 2 - Tuesday, ... *
* *
******************************************************************************/
static int calculate_dayofweek(int year, int mon, int mday)
{
static int mon_table[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
if (mon < 3)
year--;
return (year + year / 4 - year / 100 + year / 400 + mon_table[mon - 1] + mday - 1) % 7 + 1;
}
/******************************************************************************
* *
* Function: scheduler_filter_free *
* *
* Purpose: frees scheduler interval filter *
* *
* Parameters: filter - [IN] scheduler interval filter *
* *
******************************************************************************/
static void scheduler_filter_free(zbx_scheduler_filter_t *filter)
{
zbx_scheduler_filter_t *filter_next;
for (; NULL != filter; filter = filter_next)
{
filter_next = filter->next;
zbx_free(filter);
}
}
/******************************************************************************
* *
* Function: scheduler_interval_free *
* *
* Purpose: frees scheduler interval *
* *
* Parameters: interval - [IN] scheduler interval *
* *
******************************************************************************/
static void scheduler_interval_free(zbx_scheduler_interval_t *interval)
{
zbx_scheduler_interval_t *interval_next;
for (; NULL != interval; interval = interval_next)
{
interval_next = interval->next;
scheduler_filter_free(interval->mdays);
scheduler_filter_free(interval->wdays);
scheduler_filter_free(interval->hours);
scheduler_filter_free(interval->minutes);
scheduler_filter_free(interval->seconds);
zbx_free(interval);
}
}
/******************************************************************************
* *
* Function: scheduler_parse_filter_r *
* *
* Purpose: parses text string into scheduler filter *
* *
* Parameters: filter - [IN/OUT] the first filter *
* text - [IN] the text to parse *
* len - [IN/OUT] the number of characters left to parse *
* min - [IN] the minimal time unit value *
* max - [IN] the maximal time unit value *
* var_len - [IN] the maximum number of characters for a filter *
* variable (<from>, <to>, <step>) *
* *
* Return value: SUCCEED - the filter was successfully parsed *
* FAIL - otherwise *
* *
* Comments: This function recursively calls itself for each filter fragment. *
* *
******************************************************************************/
static int scheduler_parse_filter_r(zbx_scheduler_filter_t **filter, const char *text, int *len, int min, int max,
int var_len)
{
int start = 0, end = 0, step = 1;
const char *pstart, *pend;
zbx_scheduler_filter_t *filter_new;
pstart = pend = text;
while (0 != isdigit(*pend) && 0 < *len)
{
pend++;
(*len)--;
}
if (pend != pstart)
{
if (pend - pstart > var_len)
return FAIL;
if (SUCCEED != is_uint_n_range(pstart, pend - pstart, &start, sizeof(start), min, max))
return FAIL;
if ('-' == *pend)
{
pstart = pend + 1;
do
{
pend++;
(*len)--;
}
while (0 != isdigit(*pend) && 0 < *len);
/* empty or too long value, fail */
if (pend == pstart || pend - pstart > var_len)
return FAIL;
if (SUCCEED != is_uint_n_range(pstart, pend - pstart, &end, sizeof(end), min, max))
return FAIL;
if (end < start)
return FAIL;
}
else
{
/* step is valid only for defined range */
if ('/' == *pend)
return FAIL;
end = start;
}
}
else
{
start = min;
end = max;
}
if ('/' == *pend)
{
pstart = pend + 1;
do
{
pend++;
(*len)--;
}
while (0 != isdigit(*pend) && 0 < *len);
/* empty or too long step, fail */
if (pend == pstart || pend - pstart > var_len)
return FAIL;
if (SUCCEED != is_uint_n_range(pstart, pend - pstart, &step, sizeof(step), 1, end - start))
return FAIL;
}
else
{
if (pend == text)
return FAIL;
}
if (',' == *pend)
{
/* no next filter after ',' */
if (0 == --(*len))
return FAIL;
pend++;
if (SUCCEED != scheduler_parse_filter_r(filter, pend, len, min, max, var_len))
return FAIL;
}
filter_new = (zbx_scheduler_filter_t *)zbx_malloc(NULL, sizeof(zbx_scheduler_filter_t));
filter_new->start = start;
filter_new->end = end;
filter_new->step = step;
filter_new->next = *filter;
*filter = filter_new;
return SUCCEED;
}
/******************************************************************************
* *
* Function: scheduler_parse_filter *
* *
* Purpose: parses text string into scheduler filter *
* *
* Parameters: filter - [IN/OUT] the first filter *
* text - [IN] the text to parse *
* len - [IN/OUT] the number of characters left to parse *
* min - [IN] the minimal time unit value *
* max - [IN] the maximal time unit value *
* var_len - [IN] the maximum number of characters for a filter *
* variable (<from>, <to>, <step>) *
* *
* Return value: SUCCEED - the filter was successfully parsed *
* FAIL - otherwise *
* *
* Comments: This function will fail if a filter already exists. This *
* user from defining multiple filters of the same time unit in a *
* single interval. For example: h0h12 is invalid filter and its *
* parsing must fail. *
* *
******************************************************************************/
static int scheduler_parse_filter(zbx_scheduler_filter_t **filter, const char *text, int *len, int min, int max,
int var_len)
{
if (NULL != *filter)
return FAIL;
return scheduler_parse_filter_r(filter, text, len, min, max, var_len);
}
/******************************************************************************
* *
* Function: scheduler_interval_parse *
* *
* Purpose: parses scheduler interval *
* *
* Parameters: interval - [IN/OUT] the first interval *
* text - [IN] the text to parse *
* len - [IN] the text length *
* *
* Return value: SUCCEED - the interval was successfully parsed *
* FAIL - otherwise *
* *
******************************************************************************/
static int scheduler_interval_parse(zbx_scheduler_interval_t *interval, const char *text, int len)
{
int ret = SUCCEED;
if (0 == len)
return FAIL;
while (SUCCEED == ret && 0 != len)
{
int old_len = len--;
switch (*text)
{
case '\0':
return FAIL;
case 'h':
if (ZBX_SCHEDULER_FILTER_HOUR < interval->filter_level)
return FAIL;
ret = scheduler_parse_filter(&interval->hours, text + 1, &len, 0, 23, 2);
interval->filter_level = ZBX_SCHEDULER_FILTER_HOUR;
break;
case 's':
if (ZBX_SCHEDULER_FILTER_SECOND < interval->filter_level)
return FAIL;
ret = scheduler_parse_filter(&interval->seconds, text + 1, &len, 0, 59, 2);
interval->filter_level = ZBX_SCHEDULER_FILTER_SECOND;
break;
case 'w':
if ('d' != text[1])
return FAIL;
if (ZBX_SCHEDULER_FILTER_DAY < interval->filter_level)
return FAIL;
len--;
ret = scheduler_parse_filter(&interval->wdays, text + 2, &len, 1, 7, 1);
interval->filter_level = ZBX_SCHEDULER_FILTER_DAY;
break;
case 'm':
if ('d' == text[1])
{
if (ZBX_SCHEDULER_FILTER_DAY < interval->filter_level ||
NULL != interval->wdays)
{
return FAIL;
}
len--;
ret = scheduler_parse_filter(&interval->mdays, text + 2, &len, 1, 31, 2);
interval->filter_level = ZBX_SCHEDULER_FILTER_DAY;
}
else
{
if (ZBX_SCHEDULER_FILTER_MINUTE < interval->filter_level)
return FAIL;
ret = scheduler_parse_filter(&interval->minutes, text + 1, &len, 0, 59, 2);
interval->filter_level = ZBX_SCHEDULER_FILTER_MINUTE;
}
break;
default:
return FAIL;
}
text += old_len - len;
}
return ret;
}
/******************************************************************************
* *
* Function: scheduler_get_nearest_filter_value *
* *
* Purpose: gets the next nearest value that satisfies the filter chain *
* *
* Parameters: filter - [IN] the filter chain *
* value - [IN] the current value *
* [OUT] the next nearest value (>= than input value) *
* *
* Return value: SUCCEED - the next nearest value was successfully found *
* FAIL - otherwise *
* *
******************************************************************************/
static int scheduler_get_nearest_filter_value(const zbx_scheduler_filter_t *filter, int *value)
{
const zbx_scheduler_filter_t *filter_next = NULL;
for (; NULL != filter; filter = filter->next)
{
/* find matching filter */
if (filter->start <= *value && *value <= filter->end)
{
int next = *value, offset;
/* apply step */
offset = (next - filter->start) % filter->step;
if (0 != offset)
next += filter->step - offset;
/* succeed if the calculated value is still in filter range */
if (next <= filter->end)
{
*value = next;
return SUCCEED;
}
}
/* find the next nearest filter */
if (filter->start > *value && (NULL == filter_next || filter_next->start > filter->start))
filter_next = filter;
}
/* The value is not in a range of any filters, but we have next nearest filter. */
if (NULL != filter_next)
{
*value = filter_next->start;
return SUCCEED;
}
return FAIL;
}
/******************************************************************************
* *
* Function: scheduler_get_wday_nextcheck *
* *
* Purpose: calculates the next day that satisfies the week day filter *
* *
* Parameters: interval - [IN] the scheduler interval *
* tm - [IN/OUT] the input/output date & time *
* *
* Return value: SUCCEED - the next day was found *
* FAIL - the next day satisfying week day filter was not *
* found in the current month *
* *
******************************************************************************/
static int scheduler_get_wday_nextcheck(const zbx_scheduler_interval_t *interval, struct tm *tm)
{
int value_now, value_next;
if (NULL == interval->wdays)
return SUCCEED;
value_now = value_next = calculate_dayofweek(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
/* get the nearest week day from the current week day*/
if (SUCCEED != scheduler_get_nearest_filter_value(interval->wdays, &value_next))
{
/* in the case of failure move month day to the next week, reset week day and try again */
tm->tm_mday += 7 - value_now + 1;
value_now = value_next = 1;
if (SUCCEED != scheduler_get_nearest_filter_value(interval->wdays, &value_next))
{
/* a valid week day filter must always match some day of a new week */
THIS_SHOULD_NEVER_HAPPEN;
return FAIL;
}
}
/* adjust the month day by the week day offset */
tm->tm_mday += value_next - value_now;
/* check if the resulting month day is valid */
return (tm->tm_mday <= zbx_day_in_month(tm->tm_year + 1970, tm->tm_mon + 1) ? SUCCEED : FAIL);
}
/******************************************************************************
* *
* Function: scheduler_validate_wday_filter *
* *
* Purpose: checks if the specified date satisfies week day filter *
* *
* Parameters: interval - [IN] the scheduler interval *
* tm - [IN] the date & time to validate *
* *
* Return value: SUCCEED - the input date satisfies week day filter *
* FAIL - otherwise *
* *
******************************************************************************/
static int scheduler_validate_wday_filter(const zbx_scheduler_interval_t *interval, struct tm *tm)
{
const zbx_scheduler_filter_t *filter;
int value;
if (NULL == interval->wdays)
return SUCCEED;
value = calculate_dayofweek(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
/* check if the value match week day filter */
for (filter = interval->wdays; NULL != filter; filter = filter->next)
{
if (filter->start <= value && value <= filter->end)
{
int next = value, offset;
/* apply step */
offset = (next - filter->start) % filter->step;
if (0 != offset)
next += filter->step - offset;
/* succeed if the calculated value is still in filter range */
if (next <= filter->end)
return SUCCEED;
}
}
return FAIL;
}
/******************************************************************************
* *
* Function: scheduler_get_day_nextcheck *
* *
* Purpose: calculates the next day that satisfies month and week day filters *
* *
* Parameters: interval - [IN] the scheduler interval *
* tm - [IN/OUT] the input/output date & time *
* *
* Return value: SUCCEED - the next day was found *
* FAIL - the next day satisfying day filters was not *
* found in the current month *
* *
******************************************************************************/
static int scheduler_get_day_nextcheck(const zbx_scheduler_interval_t *interval, struct tm *tm)
{
int tmp;
/* first check if the provided tm structure has valid date format */
if (FAIL == zbx_utc_time(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
&tmp))
{
return FAIL;
}
if (NULL == interval->mdays)
return scheduler_get_wday_nextcheck(interval, tm);
/* iterate through month days until week day filter matches or we have ran out of month days */
while (SUCCEED == scheduler_get_nearest_filter_value(interval->mdays, &tm->tm_mday))
{
/* check if the date is still valid - we haven't ran out of month days */
if (tm->tm_mday > zbx_day_in_month(tm->tm_year + 1970, tm->tm_mon + 1))
break;
if (SUCCEED == scheduler_validate_wday_filter(interval, tm))
return SUCCEED;
tm->tm_mday++;
/* check if the date is still valid - we haven't ran out of month days */
if (tm->tm_mday > zbx_day_in_month(tm->tm_year + 1970, tm->tm_mon + 1))
break;
}
return FAIL;
}
/******************************************************************************
* *
* Function: scheduler_get_filter_nextcheck *
* *
* Purpose: calculates the time/day that satisfies the specified filter *
* *
* Parameters: interval - [IN] the scheduler interval *
* level - [IN] the filter level, see ZBX_SCHEDULER_FILTER_* *
* defines *
* tm - [IN/OUT] the input/output date & time *
* *
* Return value: SUCCEED - the next time/day was found *
* FAIL - the next time/day was not found on the current *
* filter level *
* *
******************************************************************************/
static int scheduler_get_filter_nextcheck(const zbx_scheduler_interval_t *interval, int level, struct tm *tm)
{
const zbx_scheduler_filter_t *filter;
int max, *value;
/* initialize data depending on filter level */
switch (level)
{
case ZBX_SCHEDULER_FILTER_DAY:
return scheduler_get_day_nextcheck(interval, tm);
case ZBX_SCHEDULER_FILTER_HOUR:
max = 23;
filter = interval->hours;
value = &tm->tm_hour;
break;
case ZBX_SCHEDULER_FILTER_MINUTE:
max = 59;
filter = interval->minutes;
value = &tm->tm_min;
break;
case ZBX_SCHEDULER_FILTER_SECOND:
max = 59;
filter = interval->seconds;
value = &tm->tm_sec;
break;
default:
THIS_SHOULD_NEVER_HAPPEN;
return FAIL;
}
if (max < *value)
return FAIL;
/* handle unspecified (default) filter */
if (NULL == filter)
{
/* Empty filter matches all valid values if the filter level is less than */
/* interval filter level. For example if interval filter level is minutes - m30, */
/* then hour filter matches all hours. */
if (interval->filter_level > level)
return SUCCEED;
/* If the filter level is greater than interval filter level, then filter */
/* matches only 0 value. For example if interval filter level is minutes - m30, */
/* then seconds filter matches the 0th second. */
return 0 == *value ? SUCCEED : FAIL;
}
return scheduler_get_nearest_filter_value(filter, value);
}
/******************************************************************************
* *
* Function: scheduler_apply_day_filter *
* *
* Purpose: applies day filter to the specified time/day calculating the next *
* scheduled check *
* *
* Parameters: interval - [IN] the scheduler interval *
* tm - [IN/OUT] the input/output date & time *
* *
******************************************************************************/
static void scheduler_apply_day_filter(zbx_scheduler_interval_t *interval, struct tm *tm)
{
int day = tm->tm_mday, mon = tm->tm_mon, year = tm->tm_year;
while (SUCCEED != scheduler_get_filter_nextcheck(interval, ZBX_SCHEDULER_FILTER_DAY, tm))
{
if (11 < ++tm->tm_mon)
{
tm->tm_mon = 0;
tm->tm_year++;
}
tm->tm_mday = 1;
}
/* reset hours, minutes and seconds if the day has been changed */
if (tm->tm_mday != day || tm->tm_mon != mon || tm->tm_year != year)
{
tm->tm_hour = 0;
tm->tm_min = 0;
tm->tm_sec = 0;
}
}
/******************************************************************************
* *
* Function: scheduler_apply_hour_filter *
* *
* Purpose: applies hour filter to the specified time/day calculating the *
* next scheduled check *
* *
* Parameters: interval - [IN] the scheduler interval *
* tm - [IN/OUT] the input/output date & time *
* *
******************************************************************************/
static void scheduler_apply_hour_filter(zbx_scheduler_interval_t *interval, struct tm *tm)
{
int hour = tm->tm_hour;
while (SUCCEED != scheduler_get_filter_nextcheck(interval, ZBX_SCHEDULER_FILTER_HOUR, tm))
{
tm->tm_mday++;
tm->tm_hour = 0;
/* day has been changed, we have to reapply day filter */
scheduler_apply_day_filter(interval, tm);
}
/* reset minutes and seconds if hours has been changed */
if (tm->tm_hour != hour)
{
tm->tm_min = 0;
tm->tm_sec = 0;
}
}
/******************************************************************************
* *
* Function: scheduler_apply_minute_filter *
* *
* Purpose: applies minute filter to the specified time/day calculating the *
* next scheduled check *
* *
* Parameters: interval - [IN] the scheduler interval *
* tm - [IN/OUT] the input/output date & time *
* *
******************************************************************************/
static void scheduler_apply_minute_filter(zbx_scheduler_interval_t *interval, struct tm *tm)
{
int min = tm->tm_min;
while (SUCCEED != scheduler_get_filter_nextcheck(interval, ZBX_SCHEDULER_FILTER_MINUTE, tm))
{
tm->tm_hour++;
tm->tm_min = 0;
/* hours have been changed, we have to reapply hour filter */
scheduler_apply_hour_filter(interval, tm);
}
/* reset seconds if minutes has been changed */
if (tm->tm_min != min)
tm->tm_sec = 0;
}
/******************************************************************************
* *
* Function: scheduler_apply_second_filter *
* *
* Purpose: applies second filter to the specified time/day calculating the *
* next scheduled check *
* *
* Parameters: interval - [IN] the scheduler interval *
* tm - [IN/OUT] the input/output date & time *
* *
******************************************************************************/
static void scheduler_apply_second_filter(zbx_scheduler_interval_t *interval, struct tm *tm)
{
while (SUCCEED != scheduler_get_filter_nextcheck(interval, ZBX_SCHEDULER_FILTER_SECOND, tm))
{
tm->tm_min++;
tm->tm_sec = 0;
/* minutes have been changed, we have to reapply minute filter */
scheduler_apply_minute_filter(interval, tm);
}
}
/******************************************************************************
* *
* Function: scheduler_find_dst_change *
* *
* Purpose: finds daylight saving change time inside specified time period *
* *
* Parameters: time_start - [IN] the time period start *
* time_end - [IN] the time period end *
* *
* Return Value: Time when the daylight saving changes should occur. *
* *
* Comments: The calculated time is cached and reused if it first the *
* specified period. *
* *
******************************************************************************/
static time_t scheduler_find_dst_change(time_t time_start, time_t time_end)
{
static time_t time_dst = 0;
struct tm *tm;
time_t time_mid;
int start, end, mid, dst_start;
if (time_dst < time_start || time_dst > time_end)
{
/* assume that daylight saving will change only on 0 seconds */
start = time_start / 60;
end = time_end / 60;
tm = localtime(&time_start);
dst_start = tm->tm_isdst;
while (end > start + 1)
{
mid = (start + end) / 2;
time_mid = mid * 60;
tm = localtime(&time_mid);
if (tm->tm_isdst == dst_start)
start = mid;
else
end = mid;
}
time_dst = end * 60;
}
return time_dst;
}
/******************************************************************************
* *
* Function: scheduler_tm_inc *
* *
* Purpose: increment struct tm value by one second *
* *
* Parameters: tm - [IN/OUT] the tm structure to increment *
* *
******************************************************************************/
static void scheduler_tm_inc(struct tm *tm)
{
if (60 > ++tm->tm_sec)
return;
tm->tm_sec = 0;
if (60 > ++tm->tm_min)
return;
tm->tm_min = 0;
if (24 > ++tm->tm_hour)
return;
tm->tm_hour = 0;
if (zbx_day_in_month(tm->tm_year + 1900, tm->tm_mon + 1) >= ++tm->tm_mday)
return;
tm->tm_mday = 1;
if (12 > ++tm->tm_mon)
return;
tm->tm_mon = 0;
tm->tm_year++;
return;
}
/******************************************************************************
* *
* Function: scheduler_get_nextcheck *
* *
* Purpose: finds the next timestamp satisfying one of intervals. *
* *
* Parameters: interval - [IN] the scheduler interval *
* now - [IN] the current timestamp *
* *
* Return Value: Timestamp when the next check must be scheduled. *
* *
******************************************************************************/
static time_t scheduler_get_nextcheck(zbx_scheduler_interval_t *interval, time_t now)
{
struct tm tm_start, tm, tm_dst;
time_t nextcheck = 0, current_nextcheck;
tm_start = *(localtime(&now));
for (; NULL != interval; interval = interval->next)
{
tm = tm_start;
do
{
scheduler_tm_inc(&tm);
scheduler_apply_day_filter(interval, &tm);
scheduler_apply_hour_filter(interval, &tm);
scheduler_apply_minute_filter(interval, &tm);
scheduler_apply_second_filter(interval, &tm);
tm.tm_isdst = tm_start.tm_isdst;
}
while (-1 == (current_nextcheck = mktime(&tm)));
tm_dst = *(localtime(¤t_nextcheck));
if (tm_dst.tm_isdst != tm_start.tm_isdst)
{
int dst = tm_dst.tm_isdst;
time_t time_dst;
time_dst = scheduler_find_dst_change(now, current_nextcheck);
tm_dst = *localtime(&time_dst);
scheduler_apply_day_filter(interval, &tm_dst);
scheduler_apply_hour_filter(interval, &tm_dst);
scheduler_apply_minute_filter(interval, &tm_dst);
scheduler_apply_second_filter(interval, &tm_dst);
tm_dst.tm_isdst = dst;
current_nextcheck = mktime(&tm_dst);
}
if (0 == nextcheck || current_nextcheck < nextcheck)
nextcheck = current_nextcheck;
}
return nextcheck;
}
/******************************************************************************
* *
* Function: parse_user_macro *
* *
* Purpose: parses user macro and finds it's length *
* *
* Parameters: str - [IN] string to check *
* len - [OUT] length of macro *
* *
* Return Value: *
* SUCCEED - the macro was parsed successfully. *
* FAIL - the macro parsing failed, the content of output variables *
* is not defined. *
* *
******************************************************************************/
static int parse_user_macro(const char *str, int *len)
{
int macro_r, context_l, context_r;
if ('{' != *str || '$' != *(str + 1) || SUCCEED != zbx_user_macro_parse(str, ¯o_r, &context_l, &context_r))
return FAIL;
*len = macro_r + 1;
return SUCCEED;
}
/******************************************************************************
* *
* Function: parse_simple_interval *
* *
* Purpose: parses user macro and finds it's length *
* *
* Parameters: str - [IN] string to check *
* len - [OUT] length simple interval string until separator *
* sep - [IN] separator to calculate length *
* value - [OUT] interval value *
* *
* Return Value: *
* SUCCEED - the macro was parsed successfully. *
* FAIL - the macro parsing failed, the content of output variables *
* is not defined. *
* *
******************************************************************************/
static int parse_simple_interval(const char *str, int *len, char sep, int *value)
{
const char *delim;
if (SUCCEED != is_time_suffix(str, value,
(int)(NULL == (delim = strchr(str, sep)) ? ZBX_LENGTH_UNLIMITED : delim - str)))
{
return FAIL;
}
*len = NULL == delim ? (int)strlen(str) : delim - str;
return SUCCEED;
}
/******************************************************************************
* *
* Function: zbx_validate_interval *
* *
* Purpose: validate update interval, flexible and scheduling intervals *
* *
* Parameters: str - [IN] string to check *
* error - [OUT] validation error *
* *
* Return Value: *
* SUCCEED - parsed successfully. *
* FAIL - parsing failed. *
* *
******************************************************************************/
int zbx_validate_interval(const char *str, char **error)
{
int simple_interval, interval, len, custom = 0, macro;
const char *delim;
if (SUCCEED == parse_user_macro(str, &len) && ('\0' == *(delim = str + len) || ';' == *delim))
{
if ('\0' == *delim)
delim = NULL;
simple_interval = 1;
}
else if (SUCCEED == parse_simple_interval(str, &len, ';', &simple_interval))
{
if ('\0' == *(delim = str + len))
delim = NULL;
}
else
{
*error = zbx_dsprintf(*error, "Invalid update interval \"%.*s\".",
NULL == (delim = strchr(str, ';')) ? (int)strlen(str) : (int)(delim - str), str);
return FAIL;
}
while (NULL != delim)
{
str = delim + 1;
if ((SUCCEED == (macro = parse_user_macro(str, &len)) ||
SUCCEED == parse_simple_interval(str, &len, '/', &interval)) &&
'/' == *(delim = str + len))
{
zbx_time_period_t period;
custom = 1;
if (SUCCEED == macro)
interval = 1;
if (0 == interval && 0 == simple_interval)
{
*error = zbx_dsprintf(*error, "Invalid flexible interval \"%.*s\".", (int)(delim - str),
str);
return FAIL;
}
str = delim + 1;
if (SUCCEED == parse_user_macro(str, &len) && ('\0' == *(delim = str + len) || ';' == *delim))
{
if ('\0' == *delim)
delim = NULL;
continue;
}
if (SUCCEED == time_period_parse(&period, str,
NULL == (delim = strchr(str, ';')) ? (int)strlen(str) : (int)(delim - str)))
{
continue;
}
*error = zbx_dsprintf(*error, "Invalid flexible period \"%.*s\".",
NULL == delim ? (int)strlen(str) : (int)(delim - str), str);
return FAIL;
}
else
{
zbx_scheduler_interval_t *new_interval;
custom = 1;
if (SUCCEED == macro && ('\0' == *(delim = str + len) || ';' == *delim))
{
if ('\0' == *delim)
delim = NULL;
continue;
}
new_interval = (zbx_scheduler_interval_t *)zbx_malloc(NULL, sizeof(zbx_scheduler_interval_t));
memset(new_interval, 0, sizeof(zbx_scheduler_interval_t));
if (SUCCEED == scheduler_interval_parse(new_interval, str,
NULL == (delim = strchr(str, ';')) ? (int)strlen(str) : (int)(delim - str)))
{
scheduler_interval_free(new_interval);
continue;
}
scheduler_interval_free(new_interval);
*error = zbx_dsprintf(*error, "Invalid custom interval \"%.*s\".",
NULL == delim ? (int)strlen(str) : (int)(delim - str), str);
return FAIL;
}
}
if ((0 == custom && 0 == simple_interval) || SEC_PER_DAY < simple_interval)
{
*error = zbx_dsprintf(*error, "Invalid update interval \"%d\"", simple_interval);
return FAIL;
}
return SUCCEED;
}
/******************************************************************************
* *
* Function: zbx_interval_preproc *
* *
* Purpose: parses item and low-level discovery rule update intervals *
* *
* Parameters: interval_str - [IN] update interval string to parse *
* simple_interval - [OUT] simple update interval *
* custom_intervals - [OUT] flexible and scheduling intervals *
* error - [OUT] error message *
* *
* Return value: SUCCEED - intervals are valid *
* FAIL - otherwise *
* *
* Comments: !!! Don't forget to sync code with PHP !!! *
* Supported format: *
* SimpleInterval, {";", FlexibleInterval | SchedulingInterval}; *
* *
******************************************************************************/
int zbx_interval_preproc(const char *interval_str, int *simple_interval, zbx_custom_interval_t **custom_intervals,
char **error)
{
zbx_flexible_interval_t *flexible = NULL;
zbx_scheduler_interval_t *scheduling = NULL;
const char *delim, *interval_type;
if (SUCCEED != is_time_suffix(interval_str, simple_interval,
(int)(NULL == (delim = strchr(interval_str, ';')) ? ZBX_LENGTH_UNLIMITED : delim - interval_str)))
{
interval_type = "update";
goto fail;
}
if (NULL == custom_intervals) /* caller wasn't interested in custom intervals, don't parse them */
return SUCCEED;
while (NULL != delim)
{
interval_str = delim + 1;
delim = strchr(interval_str, ';');
if (0 != isdigit(*interval_str))
{
zbx_flexible_interval_t *new_interval;
new_interval = (zbx_flexible_interval_t *)zbx_malloc(NULL, sizeof(zbx_flexible_interval_t));
if (SUCCEED != flexible_interval_parse(new_interval, interval_str,
(NULL == delim ? (int)strlen(interval_str) : (int)(delim - interval_str))) ||
(0 == *simple_interval && 0 == new_interval->delay))
{
zbx_free(new_interval);
interval_type = "flexible";
goto fail;
}
new_interval->next = flexible;
flexible = new_interval;
}
else
{
zbx_scheduler_interval_t *new_interval;
new_interval = (zbx_scheduler_interval_t *)zbx_malloc(NULL, sizeof(zbx_scheduler_interval_t));
memset(new_interval, 0, sizeof(zbx_scheduler_interval_t));
if (SUCCEED != scheduler_interval_parse(new_interval, interval_str,
(NULL == delim ? (int)strlen(interval_str) : (int)(delim - interval_str))))
{
scheduler_interval_free(new_interval);
interval_type = "scheduling";
goto fail;
}
new_interval->next = scheduling;
scheduling = new_interval;
}
}
if ((NULL == flexible && NULL == scheduling && 0 == *simple_interval) || SEC_PER_DAY < *simple_interval)
{
interval_type = "update";
goto fail;
}
*custom_intervals = (zbx_custom_interval_t *)zbx_malloc(NULL, sizeof(zbx_custom_interval_t));
(*custom_intervals)->flexible = flexible;
(*custom_intervals)->scheduling = scheduling;
return SUCCEED;
fail:
if (NULL != error)
{
*error = zbx_dsprintf(*error, "Invalid %s interval \"%.*s\".", interval_type,
(NULL == delim ? (int)strlen(interval_str) : (int)(delim - interval_str)),
interval_str);
}
flexible_interval_free(flexible);
scheduler_interval_free(scheduling);
return FAIL;
}
/******************************************************************************
* *
* Function: zbx_custom_interval_free *
* *
* Purpose: frees custom update intervals *
* *
* Parameters: custom_intervals - [IN] custom intervals *
* *
******************************************************************************/
void zbx_custom_interval_free(zbx_custom_interval_t *custom_intervals)
{
flexible_interval_free(custom_intervals->flexible);
scheduler_interval_free(custom_intervals->scheduling);
zbx_free(custom_intervals);
}
/******************************************************************************
* *
* Function: calculate_item_nextcheck *
* *
* Purpose: calculate nextcheck timestamp for item *
* *
* Parameters: seed - [IN] the seed value applied to delay to *
* spread item checks over the delay *
* period *
* item_type - [IN] the item type *
* simple_interval - [IN] default delay value, can be overridden *
* custom_intervals - [IN] preprocessed custom intervals *
* now - [IN] current timestamp *
* *
* Return value: nextcheck value *
* *
* Author: Alexei Vladishev, Aleksandrs Saveljevs *
* *
* Comments: if item check is forbidden with delay=0 (default and flexible), *
* a timestamp very far in the future is returned *
* *
* Old algorithm: now+delay *
* New one: preserve period, if delay==5, nextcheck = 0,5,10,15,... *
* *
******************************************************************************/
int calculate_item_nextcheck(zbx_uint64_t seed, int item_type, int simple_interval,
const zbx_custom_interval_t *custom_intervals, time_t now)
{
int nextcheck = 0;
/* special processing of active items to see better view in queue */
if (ITEM_TYPE_ZABBIX_ACTIVE == item_type)
{
if (0 != simple_interval)
nextcheck = (int)now + simple_interval;
else
nextcheck = ZBX_JAN_2038;
}
else
{
int current_delay = 0, attempt = 0;
time_t next_interval, t, tmax, scheduled_check = 0;
/* first try to parse out and calculate scheduled intervals */
if (NULL != custom_intervals)
scheduled_check = scheduler_get_nextcheck(custom_intervals->scheduling, now);
/* Try to find the nearest 'nextcheck' value with condition */
/* 'now' < 'nextcheck' < 'now' + SEC_PER_YEAR. If it is not */
/* possible to check the item within a year, fail. */
t = now;
tmax = now + SEC_PER_YEAR;
while (t < tmax)
{
/* calculate 'nextcheck' value for the current interval */
if (NULL != custom_intervals)
current_delay = get_current_delay(simple_interval, custom_intervals->flexible, t);
else
current_delay = simple_interval;
if (0 != current_delay)
{
nextcheck = current_delay * (int)(t / (time_t)current_delay) +
(int)(seed % (zbx_uint64_t)current_delay);
if (0 == attempt)
{
while (nextcheck <= t)
nextcheck += current_delay;
}
else
{
while (nextcheck < t)
nextcheck += current_delay;
}
}
else
nextcheck = ZBX_JAN_2038;
if (NULL == custom_intervals)
break;
/* 'nextcheck' < end of the current interval ? */
/* the end of the current interval is the beginning of the next interval - 1 */
if (FAIL != get_next_delay_interval(custom_intervals->flexible, t, &next_interval) &&
nextcheck >= next_interval)
{
/* 'nextcheck' is beyond the current interval */
t = next_interval;
attempt++;
}
else
break; /* nextcheck is within the current interval */
}
if (0 != scheduled_check && scheduled_check < nextcheck)
nextcheck = (int)scheduled_check;
}
return nextcheck;
}
/******************************************************************************
* *
* Function: calculate_item_nextcheck_unreachable *
* *
* Purpose: calculate nextcheck timestamp for item on unreachable host *
* *
* Parameters: simple_interval - [IN] default delay value, can be overridden *
* custom_intervals - [IN] preprocessed custom intervals *
* disable_until - [IN] timestamp for next check *
* *
* Return value: nextcheck value *
* *
******************************************************************************/
int calculate_item_nextcheck_unreachable(int simple_interval, const zbx_custom_interval_t *custom_intervals,
time_t disable_until)
{
int nextcheck = 0;
time_t next_interval, tmax, scheduled_check = 0;
/* first try to parse out and calculate scheduled intervals */
if (NULL != custom_intervals)
scheduled_check = scheduler_get_nextcheck(custom_intervals->scheduling, disable_until);
/* Try to find the nearest 'nextcheck' value with condition */
/* 'now' < 'nextcheck' < 'now' + SEC_PER_YEAR. If it is not */
/* possible to check the item within a year, fail. */
nextcheck = disable_until;
tmax = disable_until + SEC_PER_YEAR;
if (NULL != custom_intervals)
{
while (nextcheck < tmax)
{
if (0 != get_current_delay(simple_interval, custom_intervals->flexible, nextcheck))
break;
/* find the flexible interval change */
if (FAIL == get_next_delay_interval(custom_intervals->flexible, nextcheck, &next_interval))
{
nextcheck = ZBX_JAN_2038;
break;
}
nextcheck = next_interval;
}
}
if (0 != scheduled_check && scheduled_check < nextcheck)
nextcheck = (int)scheduled_check;
return nextcheck;
}
/******************************************************************************
* *
* Function: calculate_proxy_nextcheck *
* *
* Purpose: calculate nextcheck timestamp for passive proxy *
* *
* Parameters: hostid - [IN] host identificator from database *
* delay - [IN] default delay value, can be overridden *
* now - [IN] current timestamp *
* *
* Return value: nextcheck value *
* *
* Author: Alexander Vladishev *
* *
******************************************************************************/
time_t calculate_proxy_nextcheck(zbx_uint64_t hostid, unsigned int delay, time_t now)
{
time_t nextcheck;
nextcheck = delay * (now / delay) + (unsigned int)(hostid % delay);
while (nextcheck <= now)
nextcheck += delay;
return nextcheck;
}
/******************************************************************************
* *
* Function: is_ip4 *
* *
* Purpose: is string IPv4 address *
* *
* Parameters: ip - string *
* *
* Return value: SUCCEED - is IPv4 address *
* FAIL - otherwise *
* *
* Author: Alexei Vladishev, Alexander Vladishev *
* *
******************************************************************************/
int is_ip4(const char *ip)
{
const char *p = ip;
int digits = 0, dots = 0, res = FAIL, octet = 0;
zabbix_log(LOG_LEVEL_DEBUG, "In %s() ip:'%s'", __func__, ip);
while ('\0' != *p)
{
if (0 != isdigit(*p))
{
octet = octet * 10 + (*p - '0');
digits++;
}
else if ('.' == *p)
{
if (0 == digits || 3 < digits || 255 < octet)
break;
digits = 0;
octet = 0;
dots++;
}
else
{
digits = 0;
break;
}
p++;
}
if (3 == dots && 1 <= digits && 3 >= digits && 255 >= octet)
res = SUCCEED;
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(res));
return res;
}
/******************************************************************************
* *
* Function: is_ip6 *
* *
* Purpose: is string IPv6 address *
* *
* Parameters: ip - string *
* *
* Return value: SUCCEED - is IPv6 address *
* FAIL - otherwise *
* *
* Author: Alexander Vladishev *
* *
******************************************************************************/
int is_ip6(const char *ip)
{
const char *p = ip, *last_colon;
int xdigits = 0, only_xdigits = 0, colons = 0, dbl_colons = 0, res;
zabbix_log(LOG_LEVEL_DEBUG, "In %s() ip:'%s'", __func__, ip);
while ('\0' != *p)
{
if (0 != isxdigit(*p))
{
xdigits++;
only_xdigits = 1;
}
else if (':' == *p)
{
if (0 == xdigits && 0 < colons)
{
/* consecutive sections of zeros are replaced with a double colon */
only_xdigits = 1;
dbl_colons++;
}
if (4 < xdigits || 1 < dbl_colons)
break;
xdigits = 0;
colons++;
}
else
{
only_xdigits = 0;
break;
}
p++;
}
if (2 > colons || 7 < colons || 1 < dbl_colons || 4 < xdigits)
res = FAIL;
else if (1 == only_xdigits)
res = SUCCEED;
else if (7 > colons && (last_colon = strrchr(ip, ':')) < p)
res = is_ip4(last_colon + 1); /* past last column is ipv4 mapped address */
else
res = FAIL;
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(res));
return res;
}
/******************************************************************************
* *
* Function: is_supported_ip *
* *
* Purpose: is string IP address of supported version *
* *
* Parameters: ip - string *
* *
* Return value: SUCCEED - is IP address *
* FAIL - otherwise *
* *
* Author: Alexander Vladishev *
* *
******************************************************************************/
int is_supported_ip(const char *ip)
{
if (SUCCEED == is_ip4(ip))
return SUCCEED;
#ifdef HAVE_IPV6
if (SUCCEED == is_ip6(ip))
return SUCCEED;
#endif
return FAIL;
}
/******************************************************************************
* *
* Function: is_ip *
* *
* Purpose: is string IP address *
* *
* Parameters: ip - string *
* *
* Return value: SUCCEED - is IP address *
* FAIL - otherwise *
* *
* Author: Alexander Vladishev *
* *
******************************************************************************/
int is_ip(const char *ip)
{
return SUCCEED == is_ip4(ip) ? SUCCEED : is_ip6(ip);
}
/******************************************************************************
* *
* Function: zbx_validate_hostname *
* *
* Purpose: check if string is a valid internet hostname *
* *
* Parameters: hostname - [IN] hostname string to be checked *
* *
* Return value: SUCCEED - could be a valid hostname, *
* FAIL - definitely not a valid hostname *
* Comments: *
* Validation is not strict. Restrictions not checked: *
* - individual label (component) length 1-63, *
* - hyphens ('-') allowed only as interior characters in labels, *
* - underscores ('_') allowed in domain name, but not in hostname. *
* *
******************************************************************************/
int zbx_validate_hostname(const char *hostname)
{
int component; /* periods ('.') are only allowed when they serve to delimit components */
int len = MAX_ZBX_DNSNAME_LEN;
const char *p;
/* the first character must be an alphanumeric character */
if (0 == isalnum(*hostname))
return FAIL;
/* check only up to the first 'len' characters, the 1st character is already successfully checked */
for (p = hostname + 1, component = 1; '\0' != *p; p++)
{
if (0 == --len) /* hostname too long */
return FAIL;
/* check for allowed characters */
if (0 != isalnum(*p) || '-' == *p || '_' == *p)
component = 1;
else if ('.' == *p && 1 == component)
component = 0;
else
return FAIL;
}
return SUCCEED;
}
/******************************************************************************
* *
* Function: ip_in_list *
* *
* Purpose: check if ip matches range of ip addresses *
* *
* Parameters: list - [IN] comma-separated list of ip ranges *
* 192.168.0.1-64,192.168.0.128,10.10.0.0/24,12fc::21 *
* ip - [IN] ip address *
* *
* Return value: FAIL - out of range, SUCCEED - within the range *
* *
******************************************************************************/
int ip_in_list(const char *list, const char *ip)
{
int ipaddress[8];
zbx_iprange_t iprange;
char *address = NULL;
size_t address_alloc = 0, address_offset;
const char *ptr;
int ret = FAIL;
zabbix_log(LOG_LEVEL_DEBUG, "In %s() list:'%s' ip:'%s'", __func__, list, ip);
if (SUCCEED != iprange_parse(&iprange, ip))
goto out;
#ifndef HAVE_IPV6
if (ZBX_IPRANGE_V6 == iprange.type)
goto out;
#endif
iprange_first(&iprange, ipaddress);
for (ptr = list; '\0' != *ptr; list = ptr + 1)
{
if (NULL == (ptr = strchr(list, ',')))
ptr = list + strlen(list);
address_offset = 0;
zbx_strncpy_alloc(&address, &address_alloc, &address_offset, list, ptr - list);
if (SUCCEED != iprange_parse(&iprange, address))
continue;
#ifndef HAVE_IPV6
if (ZBX_IPRANGE_V6 == iprange.type)
continue;
#endif
if (SUCCEED == iprange_validate(&iprange, ipaddress))
{
ret = SUCCEED;
break;
}
}
zbx_free(address);
out:
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(ret));
return ret;
}
/******************************************************************************
* *
* Function: int_in_list *
* *
* Purpose: check if integer matches a list of integers *
* *
* Parameters: list - integers [i1-i2,i3,i4,i5-i6] (10-25,45,67-699) *
* value - integer to check *
* *
* Return value: FAIL - out of period, SUCCEED - within the period *
* *
* Author: Alexei Vladishev *
* *
******************************************************************************/
int int_in_list(char *list, int value)
{
char *start = NULL, *end = NULL, c = '\0';
int i1, i2, ret = FAIL;
zabbix_log(LOG_LEVEL_DEBUG, "In %s() list:'%s' value:%d", __func__, list, value);
for (start = list; '\0' != *start;)
{
if (NULL != (end = strchr(start, ',')))
{
c = *end;
*end = '\0';
}
if (2 == sscanf(start, "%d-%d", &i1, &i2))
{
if (i1 <= value && value <= i2)
{
ret = SUCCEED;
break;
}
}
else
{
if (value == atoi(start))
{
ret = SUCCEED;
break;
}
}
if (NULL != end)
{
*end = c;
start = end + 1;
}
else
break;
}
if (NULL != end)
*end = c;
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(ret));
return ret;
}
int zbx_double_compare(double a, double b)
{
return fabs(a - b) <= ZBX_DOUBLE_EPSILON ? SUCCEED : FAIL;
}
/******************************************************************************
* *
* Function: is_double_suffix *
* *
* Purpose: check if the string is double *
* *
* Parameters: str - string to check *
* flags - extra options including: *
* ZBX_FLAG_DOUBLE_SUFFIX - allow suffixes *
* *
* Return value: SUCCEED - the string is double *
* FAIL - otherwise *
* *
* Author: Alexei Vladishev *
* *
* Comments: the function automatically processes suffixes K, M, G, T and *
* s, m, h, d, w *
* *
******************************************************************************/
int is_double_suffix(const char *str, unsigned char flags)
{
int len;
if ('-' == *str) /* check leading sign */
str++;
if (FAIL == zbx_number_parse(str, &len))
return FAIL;
if ('\0' != *(str += len) && 0 != (flags & ZBX_FLAG_DOUBLE_SUFFIX) && NULL != strchr(ZBX_UNIT_SYMBOLS, *str))
str++; /* allow valid suffix if flag is enabled */
return '\0' == *str ? SUCCEED : FAIL;
}
/******************************************************************************
* *
* Function: is_double *
* *
* Purpose: check if the string is double *
* *
* Parameters: str - string to check *
* *
* Return value: SUCCEED - the string is double *
* FAIL - otherwise *
* *
* Author: Alexei Vladishev, Aleksandrs Saveljevs *
* *
******************************************************************************/
int is_double(const char *str)
{
int len;
if ('-' == *str || '+' == *str) /* check leading sign */
str++;
if (FAIL == zbx_number_parse(str, &len))
return FAIL;
str += len;
if ('e' == *str || 'E' == *str) /* check exponential part */
{
str++;
if ('-' == *str || '+' == *str) /* check exponent sign */
str++;
if (0 == isdigit(*str)) /* check exponent */
return FAIL;
while (0 != isdigit(*str))
str++;
}
return '\0' == *str ? SUCCEED : FAIL;
}
/******************************************************************************
* *
* Function: is_time_suffix *
* *
* Purpose: check if the string is a non-negative integer with or without *
* supported time suffix *
* *
* Parameters: str - [IN] string to check *
* value - [OUT] a pointer to converted value (optional) *
* length - [IN] number of characters to validate, pass *
* ZBX_LENGTH_UNLIMITED to validate full string *
* *
* Return value: SUCCEED - the string is valid and within reasonable limits *
* FAIL - otherwise *
* *
* Author: Aleksandrs Saveljevs, Vladimir Levijev *
* *
* Comments: the function automatically processes suffixes s, m, h, d, w *
* *
******************************************************************************/
int is_time_suffix(const char *str, int *value, int length)
{
const int max = 0x7fffffff; /* minimum acceptable value for INT_MAX is 2 147 483 647 */
int len = length;
int value_tmp = 0, c, factor = 1;
if ('\0' == *str || 0 >= len || 0 == isdigit(*str))
return FAIL;
while ('\0' != *str && 0 < len && 0 != isdigit(*str))
{
c = (int)(unsigned char)(*str - '0');
if ((max - c) / 10 < value_tmp)
return FAIL; /* overflow */
value_tmp = value_tmp * 10 + c;
str++;
len--;
}
if ('\0' != *str && 0 < len)
{
switch (*str)
{
case 's':
break;
case 'm':
factor = SEC_PER_MIN;
break;
case 'h':
factor = SEC_PER_HOUR;
break;
case 'd':
factor = SEC_PER_DAY;
break;
case 'w':
factor = SEC_PER_WEEK;
break;
default:
return FAIL;
}
str++;
len--;
}
if ((ZBX_LENGTH_UNLIMITED == length && '\0' != *str) || (ZBX_LENGTH_UNLIMITED != length && 0 != len))
return FAIL;
if (max / factor < value_tmp)
return FAIL; /* overflow */
if (NULL != value)
*value = value_tmp * factor;
return SUCCEED;
}
#ifdef _WINDOWS
int _wis_uint(const wchar_t *wide_string)
{
const wchar_t *wide_char = wide_string;
if (L'\0' == *wide_char)
return FAIL;
while (L'\0' != *wide_char)
{
if (0 != iswdigit(*wide_char))
{
wide_char++;
continue;
}
return FAIL;
}
return SUCCEED;
}
#endif
/******************************************************************************
* *
* Function: is_uint_n_range *
* *
* Purpose: check if the string is unsigned integer within the specified *
* range and optionally store it into value parameter *
* *
* Parameters: str - [IN] string to check *
* n - [IN] string length or ZBX_MAX_UINT64_LEN *
* value - [OUT] a pointer to output buffer where the converted *
* value is to be written (optional, can be NULL) *
* size - [IN] size of the output buffer (optional) *
* min - [IN] the minimum acceptable value *
* max - [IN] the maximum acceptable value *
* *
* Return value: SUCCEED - the string is unsigned integer *
* FAIL - the string is not a number or its value is outside *
* the specified range *
* *
* Author: Alexander Vladishev, Andris Zeila *
* *
******************************************************************************/
int is_uint_n_range(const char *str, size_t n, void *value, size_t size, zbx_uint64_t min, zbx_uint64_t max)
{
zbx_uint64_t value_uint64 = 0, c;
const zbx_uint64_t max_uint64 = ~(zbx_uint64_t)__UINT64_C(0);
if ('\0' == *str || 0 == n || sizeof(zbx_uint64_t) < size || (0 == size && NULL != value))
return FAIL;
while ('\0' != *str && 0 < n--)
{
if (0 == isdigit(*str))
return FAIL; /* not a digit */
c = (zbx_uint64_t)(unsigned char)(*str - '0');
if ((max_uint64 - c) / 10 < value_uint64)
return FAIL; /* maximum value exceeded */
value_uint64 = value_uint64 * 10 + c;
str++;
}
if (min > value_uint64 || value_uint64 > max)
return FAIL;
if (NULL != value)
{
/* On little endian architecture the output value will be stored starting from the first bytes */
/* of 'value' buffer while on big endian architecture it will be stored starting from the last */
/* bytes. We handle it by storing the offset in the most significant byte of short value and */
/* then use the first byte as source offset. */
unsigned short value_offset = (unsigned short)((sizeof(zbx_uint64_t) - size) << 8);
memcpy(value, (unsigned char *)&value_uint64 + *((unsigned char *)&value_offset), size);
}
return SUCCEED;
}
/******************************************************************************
* *
* Function: is_hex_n_range *
* *
* Purpose: check if the string is unsigned hexadecimal integer within the *
* specified range and optionally store it into value parameter *
* *
* Parameters: str - [IN] string to check *
* n - [IN] string length *
* value - [OUT] a pointer to output buffer where the converted *
* value is to be written (optional, can be NULL) *
* size - [IN] size of the output buffer (optional) *
* min - [IN] the minimum acceptable value *
* max - [IN] the maximum acceptable value *
* *
* Return value: SUCCEED - the string is unsigned integer *
* FAIL - the string is not a hexadecimal number or its value *
* is outside the specified range *
* *
******************************************************************************/
int is_hex_n_range(const char *str, size_t n, void *value, size_t size, zbx_uint64_t min, zbx_uint64_t max)
{
zbx_uint64_t value_uint64 = 0, c;
const zbx_uint64_t max_uint64 = ~(zbx_uint64_t)__UINT64_C(0);
int len = 0;
if ('\0' == *str || 0 == n || sizeof(zbx_uint64_t) < size || (0 == size && NULL != value))
return FAIL;
while ('\0' != *str && 0 < n--)
{
if ('0' <= *str && *str <= '9')
c = *str - '0';
else if ('a' <= *str && *str <= 'f')
c = 10 + (*str - 'a');
else if ('A' <= *str && *str <= 'F')
c = 10 + (*str - 'A');
else
return FAIL; /* not a hexadecimal digit */
if (16 < ++len && (max_uint64 >> 4) < value_uint64)
return FAIL; /* maximum value exceeded */
value_uint64 = (value_uint64 << 4) + c;
str++;
}
if (min > value_uint64 || value_uint64 > max)
return FAIL;
if (NULL != value)
{
/* On little endian architecture the output value will be stored starting from the first bytes */
/* of 'value' buffer while on big endian architecture it will be stored starting from the last */
/* bytes. We handle it by storing the offset in the most significant byte of short value and */
/* then use the first byte as source offset. */
unsigned short value_offset = (unsigned short)((sizeof(zbx_uint64_t) - size) << 8);
memcpy(value, (unsigned char *)&value_uint64 + *((unsigned char *)&value_offset), size);
}
return SUCCEED;
}
/******************************************************************************
* *
* Function: is_boolean *
* *
* Purpose: check if the string is boolean *
* *
* Parameters: str - string to check *
* *
* Return value: SUCCEED - the string is boolean *
* FAIL - otherwise *
* *
* Author: Aleksandrs Saveljevs *
* *
* Comments: *
* *
******************************************************************************/
int is_boolean(const char *str, zbx_uint64_t *value)
{
int res;
if (SUCCEED == (res = is_double(str)))
*value = (0 != atof(str));
else
{
char tmp[16];
strscpy(tmp, str);
zbx_strlower(tmp);
if (SUCCEED == (res = str_in_list("true,t,yes,y,on,up,running,enabled,available,ok,master", tmp, ',')))
{
*value = 1;
}
else if (SUCCEED == (res = str_in_list("false,f,no,n,off,down,unused,disabled,unavailable,err,slave",
tmp, ',')))
{
*value = 0;
}
}
return res;
}
/******************************************************************************
* *
* Function: is_uoct *
* *
* Purpose: check if the string is unsigned octal *
* *
* Parameters: str - string to check *
* *
* Return value: SUCCEED - the string is unsigned octal *
* FAIL - otherwise *
* *
* Author: Alexander Vladishev *
* *
******************************************************************************/
int is_uoct(const char *str)
{
int res = FAIL;
while (' ' == *str) /* trim left spaces */
str++;
for (; '\0' != *str; str++)
{
if (*str < '0' || *str > '7')
break;
res = SUCCEED;
}
while (' ' == *str) /* check right spaces */
str++;
if ('\0' != *str)
return FAIL;
return res;
}
/******************************************************************************
* *
* Function: is_uhex *
* *
* Purpose: check if the string is unsigned hexadecimal representation of *
* data in the form "0-9, a-f or A-F" *
* *
* Parameters: str - string to check *
* *
* Return value: SUCCEED - the string is unsigned hexadecimal *
* FAIL - otherwise *
* *
* Author: Alexander Vladishev *
* *
******************************************************************************/
int is_uhex(const char *str)
{
int res = FAIL;
while (' ' == *str) /* trim left spaces */
str++;
for (; '\0' != *str; str++)
{
if (0 == isxdigit(*str))
break;
res = SUCCEED;
}
while (' ' == *str) /* check right spaces */
str++;
if ('\0' != *str)
return FAIL;
return res;
}
/******************************************************************************
* *
* Function: is_hex_string *
* *
* Purpose: check if the string is a hexadecimal representation of data in *
* the form "F4 CE 46 01 0C 44 8B F4\nA0 2C 29 74 5D 3F 13 49\n" *
* *
* Parameters: str - string to check *
* *
* Return value: SUCCEED - the string is formatted like the example above *
* FAIL - otherwise *
* *
* Author: Aleksandrs Saveljevs *
* *
******************************************************************************/
int is_hex_string(const char *str)
{
if ('\0' == *str)
return FAIL;
while ('\0' != *str)
{
if (0 == isxdigit(*str))
return FAIL;
if (0 == isxdigit(*(str + 1)))
return FAIL;
if ('\0' == *(str + 2))
break;
if (' ' != *(str + 2) && '\n' != *(str + 2))
return FAIL;
str += 3;
}
return SUCCEED;
}
/******************************************************************************
* *
* Function: get_nearestindex *
* *
* Purpose: get nearest index position of sorted elements in array *
* *
* Parameters: p - pointer to array of elements *
* sz - element size *
* num - number of elements *
* id - index to look for *
* *
* Return value: index at which it would be possible to insert the element so *
* that the array is still sorted *
* *
******************************************************************************/
int get_nearestindex(const void *p, size_t sz, int num, zbx_uint64_t id)
{
int first_index, last_index, index;
zbx_uint64_t element_id;
if (0 == num)
return 0;
first_index = 0;
last_index = num - 1;
while (1)
{
index = first_index + (last_index - first_index) / 2;
if (id == (element_id = *(const zbx_uint64_t *)((const char *)p + index * sz)))
return index;
if (last_index == first_index)
{
if (element_id < id)
index++;
return index;
}
if (element_id < id)
first_index = index + 1;
else
last_index = index;
}
}
/******************************************************************************
* *
* Function: uint64_array_add *
* *
* Purpose: add uint64 value to dynamic array *
* *
* Author: Alexander Vladishev *
* *
******************************************************************************/
int uint64_array_add(zbx_uint64_t **values, int *alloc, int *num, zbx_uint64_t value, int alloc_step)
{
int index;
index = get_nearestindex(*values, sizeof(zbx_uint64_t), *num, value);
if (index < (*num) && (*values)[index] == value)
return index;
if (*alloc == *num)
{
if (0 == alloc_step)
{
zbx_error("Unable to reallocate buffer");
assert(0);
}
*alloc += alloc_step;
*values = (zbx_uint64_t *)zbx_realloc(*values, *alloc * sizeof(zbx_uint64_t));
}
memmove(&(*values)[index + 1], &(*values)[index], sizeof(zbx_uint64_t) * (*num - index));
(*values)[index] = value;
(*num)++;
return index;
}
/******************************************************************************
* *
* Function: uint64_array_exists *
* *
* Author: Alexander Vladishev *
* *
******************************************************************************/
int uint64_array_exists(const zbx_uint64_t *values, int num, zbx_uint64_t value)
{
int index;
index = get_nearestindex(values, sizeof(zbx_uint64_t), num, value);
if (index < num && values[index] == value)
return SUCCEED;
return FAIL;
}
/******************************************************************************
* *
* Function: uint64_array_remove *
* *
* Purpose: remove uint64 values from array *
* *
* Author: Alexander Vladishev *
* *
******************************************************************************/
void uint64_array_remove(zbx_uint64_t *values, int *num, const zbx_uint64_t *rm_values, int rm_num)
{
int rindex, index;
for (rindex = 0; rindex < rm_num; rindex++)
{
index = get_nearestindex(values, sizeof(zbx_uint64_t), *num, rm_values[rindex]);
if (index == *num || values[index] != rm_values[rindex])
continue;
memmove(&values[index], &values[index + 1], sizeof(zbx_uint64_t) * ((*num) - index - 1));
(*num)--;
}
}
zbx_uint64_t suffix2factor(char c)
{
switch (c)
{
case 'K':
return ZBX_KIBIBYTE;
case 'M':
return ZBX_MEBIBYTE;
case 'G':
return ZBX_GIBIBYTE;
case 'T':
return ZBX_TEBIBYTE;
case 's':
return 1;
case 'm':
return SEC_PER_MIN;
case 'h':
return SEC_PER_HOUR;
case 'd':
return SEC_PER_DAY;
case 'w':
return SEC_PER_WEEK;
default:
return 1;
}
}
/******************************************************************************
* *
* Function: str2uint64 *
* *
* Purpose: convert string to 64bit unsigned integer *
* *
* Parameters: str - string to convert *
* value - a pointer to converted value *
* *
* Return value: SUCCEED - the string is unsigned integer *
* FAIL - otherwise *
* *
* Author: Alexander Vladishev *
* *
* Comments: the function automatically processes suffixes K, M, G, T *
* *
******************************************************************************/
int str2uint64(const char *str, const char *suffixes, zbx_uint64_t *value)
{
size_t sz;
const char *p;
int ret;
zbx_uint64_t factor = 1;
sz = strlen(str);
p = str + sz - 1;
if (NULL != strchr(suffixes, *p))
{
factor = suffix2factor(*p);
sz--;
}
if (SUCCEED == (ret = is_uint64_n(str, sz, value)))
*value *= factor;
return ret;
}
/******************************************************************************
* *
* Function: str2double *
* *
* Purpose: convert string to double *
* *
* Parameters: str - string to convert *
* *
* Return value: converted double value *
* *
* Author: Alexei Vladishev *
* *
* Comments: the function automatically processes suffixes K, M, G, T and *
* s, m, h, d, w *
* *
******************************************************************************/
double str2double(const char *str)
{
size_t sz;
sz = strlen(str) - 1;
return atof(str) * suffix2factor(str[sz]);
}
/******************************************************************************
* *
* Function: is_hostname_char *
* *
* Return value: SUCCEED - the char is allowed in the host name *
* FAIL - otherwise *
* *
* Author: Alexander Vladishev *
* *
* Comments: in host name allowed characters: '0-9a-zA-Z. _-' *
* !!! Don't forget to sync the code with PHP !!! *
* *
******************************************************************************/
int is_hostname_char(unsigned char c)
{
if (0 != isalnum(c))
return SUCCEED;
if (c == '.' || c == ' ' || c == '_' || c == '-')
return SUCCEED;
return FAIL;
}
/******************************************************************************
* *
* Function: is_key_char *
* *
* Return value: SUCCEED - the char is allowed in the item key *
* FAIL - otherwise *
* *
* Author: Alexander Vladishev *
* *
* Comments: in key allowed characters: '0-9a-zA-Z._-' *
* !!! Don't forget to sync the code with PHP !!! *
* *
******************************************************************************/
int is_key_char(unsigned char c)
{
if (0 != isalnum(c))
return SUCCEED;
if (c == '.' || c == '_' || c == '-')
return SUCCEED;
return FAIL;
}
/******************************************************************************
* *
* Function: is_function_char *
* *
* Return value: SUCCEED - the char is allowed in the trigger function *
* FAIL - otherwise *
* *
* Author: Alexander Vladishev *
* *
* Comments: in trigger function allowed characters: 'a-z' *
* !!! Don't forget to sync the code with PHP !!! *
* *
******************************************************************************/
int is_function_char(unsigned char c)
{
if (0 != islower(c))
return SUCCEED;
return FAIL;
}
/******************************************************************************
* *
* Function: is_macro_char *
* *
* Return value: SUCCEED - the char is allowed in the macro name *
* FAIL - otherwise *
* *
* Author: Alexander Vladishev *
* *
* Comments: allowed characters in macro names: '0-9A-Z._' *
* !!! Don't forget to sync the code with PHP !!! *
* *
******************************************************************************/
int is_macro_char(unsigned char c)
{
if (0 != isupper(c))
return SUCCEED;
if ('.' == c || '_' == c)
return SUCCEED;
if (0 != isdigit(c))
return SUCCEED;
return FAIL;
}
/******************************************************************************
* *
* Function: is_discovery_macro *
* *
* Purpose: checks if the name is a valid discovery macro *
* *
* Return value: SUCCEED - the name is a valid discovery macro *
* FAIL - otherwise *
* *
******************************************************************************/
int is_discovery_macro(const char *name)
{
if ('{' != *name++ || '#' != *name++)
return FAIL;
do
{
if (SUCCEED != is_macro_char(*name++))
return FAIL;
} while ('}' != *name);
if ('\0' != name[1])
return FAIL;
return SUCCEED;
}
/******************************************************************************
* *
* Function: is_time_function *
* *
* Return value: SUCCEED - given function is time-based *
* FAIL - otherwise *
* *
* Author: Aleksandrs Saveljevs *
* *
******************************************************************************/
int is_time_function(const char *func)
{
return str_in_list("nodata,date,dayofmonth,dayofweek,time,now", func, ',');
}
/******************************************************************************
* *
* Function: is_snmp_type *
* *
* Return value: SUCCEED - the given type is one of regular SNMP types *
* FAIL - otherwise *
* *
* Author: Aleksandrs Saveljevs *
* *
******************************************************************************/
int is_snmp_type(unsigned char type)
{
return ITEM_TYPE_SNMPv1 == type || ITEM_TYPE_SNMPv2c == type || ITEM_TYPE_SNMPv3 == type ? SUCCEED : FAIL;
}
/******************************************************************************
* *
* Function: make_hostname *
* *
* Purpose: replace all not-allowed hostname characters in the string *
* *
* Parameters: host - the target C-style string *
* *
* Author: Dmitry Borovikov *
* *
* Comments: the string must be null-terminated, otherwise not secure! *
* *
******************************************************************************/
void make_hostname(char *host)
{
char *c;
assert(host);
for (c = host; '\0' != *c; ++c)
{
if (FAIL == is_hostname_char(*c))
*c = '_';
}
}
/******************************************************************************
* *
* Function: get_interface_type_by_item_type *
* *
* Purpose: *
* *
* Parameters: *
* *
* Return value: Interface type *
* *
* Author: Alexander Vladishev *
* *
* Comments: !!! Don't forget to sync the code with PHP !!! *
* *
******************************************************************************/
unsigned char get_interface_type_by_item_type(unsigned char type)
{
switch (type)
{
case ITEM_TYPE_ZABBIX:
return INTERFACE_TYPE_AGENT;
case ITEM_TYPE_SNMPv1:
case ITEM_TYPE_SNMPv2c:
case ITEM_TYPE_SNMPv3:
case ITEM_TYPE_SNMPTRAP:
return INTERFACE_TYPE_SNMP;
case ITEM_TYPE_IPMI:
return INTERFACE_TYPE_IPMI;
case ITEM_TYPE_JMX:
return INTERFACE_TYPE_JMX;
case ITEM_TYPE_SIMPLE:
case ITEM_TYPE_EXTERNAL:
case ITEM_TYPE_SSH:
case ITEM_TYPE_TELNET:
case ITEM_TYPE_HTTPAGENT:
return INTERFACE_TYPE_ANY;
default:
return INTERFACE_TYPE_UNKNOWN;
}
}
/******************************************************************************
* *
* Function: calculate_sleeptime *
* *
* Purpose: calculate sleep time for Zabbix processes *
* *
* Parameters: nextcheck - [IN] next check or -1 (FAIL) if nothing to do *
* max_sleeptime - [IN] maximum sleep time, in seconds *
* *
* Return value: sleep time, in seconds *
* *
* Author: Alexander Vladishev *
* *
******************************************************************************/
int calculate_sleeptime(int nextcheck, int max_sleeptime)
{
int sleeptime;
if (FAIL == nextcheck)
return max_sleeptime;
sleeptime = nextcheck - (int)time(NULL);
if (sleeptime < 0)
return 0;
if (sleeptime > max_sleeptime)
return max_sleeptime;
return sleeptime;
}
/******************************************************************************
* *
* Function: parse_serveractive_element *
* *
* Purpose: parse a ServerActive element like "IP<:port>" or "[IPv6]<:port>" *
* *
******************************************************************************/
int parse_serveractive_element(char *str, char **host, unsigned short *port, unsigned short port_default)
{
#ifdef HAVE_IPV6
char *r1 = NULL;
#endif
char *r2 = NULL;
int res = FAIL;
*port = port_default;
#ifdef HAVE_IPV6
if ('[' == *str)
{
str++;
if (NULL == (r1 = strchr(str, ']')))
goto fail;
if (':' != r1[1] && '\0' != r1[1])
goto fail;
if (':' == r1[1] && SUCCEED != is_ushort(r1 + 2, port))
goto fail;
*r1 = '\0';
if (SUCCEED != is_ip6(str))
goto fail;
*host = zbx_strdup(*host, str);
}
else if (SUCCEED == is_ip6(str))
{
*host = zbx_strdup(*host, str);
}
else
{
#endif
if (NULL != (r2 = strchr(str, ':')))
{
if (SUCCEED != is_ushort(r2 + 1, port))
goto fail;
*r2 = '\0';
}
*host = zbx_strdup(NULL, str);
#ifdef HAVE_IPV6
}
#endif
res = SUCCEED;
fail:
#ifdef HAVE_IPV6
if (NULL != r1)
*r1 = ']';
#endif
if (NULL != r2)
*r2 = ':';
return res;
}
void zbx_alarm_flag_set(void)
{
zbx_timed_out = 1;
}
void zbx_alarm_flag_clear(void)
{
zbx_timed_out = 0;
}
#if !defined(_WINDOWS)
unsigned int zbx_alarm_on(unsigned int seconds)
{
zbx_alarm_flag_clear();
return alarm(seconds);
}
unsigned int zbx_alarm_off(void)
{
unsigned int ret;
ret = alarm(0);
zbx_alarm_flag_clear();
return ret;
}
#endif
int zbx_alarm_timed_out(void)
{
return (0 == zbx_timed_out ? FAIL : SUCCEED);
}
/******************************************************************************
* *
* Function: zbx_create_token *
* *
* Purpose: creates semi-unique token based on the seed and current timestamp *
* *
* Parameters: seed - [IN] the seed *
* *
* Return value: Hexadecimal token string, must be freed by caller *
* *
* Comments: if you change token creation algorithm do not forget to adjust *
* ZBX_DATA_SESSION_TOKEN_SIZE definition *
* *
******************************************************************************/
char *zbx_create_token(zbx_uint64_t seed)
{
const char *hex = "0123456789abcdef";
zbx_timespec_t ts;
md5_state_t state;
md5_byte_t hash[MD5_DIGEST_SIZE];
int i;
char *token, *ptr;
ptr = token = (char *)zbx_malloc(NULL, ZBX_DATA_SESSION_TOKEN_SIZE + 1);
zbx_timespec(&ts);
zbx_md5_init(&state);
zbx_md5_append(&state, (const md5_byte_t *)&seed, (int)sizeof(seed));
zbx_md5_append(&state, (const md5_byte_t *)&ts, (int)sizeof(ts));
zbx_md5_finish(&state, hash);
for (i = 0; i < MD5_DIGEST_SIZE; i++)
{
*ptr++ = hex[hash[i] >> 4];
*ptr++ = hex[hash[i] & 15];
}
*ptr = '\0';
return token;
}
/******************************************************************************
* *
* Function: zbx_update_env *
* *
* Purpose: throttling of update "/etc/resolv.conf" and "stdio" to the new *
* log file after rotation *
* *
* Parameters: time_now - [IN] the time for compare in seconds *
* *
******************************************************************************/
void zbx_update_env(double time_now)
{
static double time_update = 0;
/* handle /etc/resolv.conf update and log rotate less often than once a second */
if (1.0 < time_now - time_update)
{
time_update = time_now;
zbx_handle_log();
#if !defined(_WINDOWS) && defined(HAVE_RESOLV_H)
zbx_update_resolver_conf();
#endif
}
}
/******************************************************************************
* *
* Function: zbx_dc_get_agent_item_nextcheck *
* *
* Purpose: calculate item nextcheck for zabix agent type items *
* *
******************************************************************************/
int zbx_get_agent_item_nextcheck(zbx_uint64_t itemid, const char *delay, unsigned char state, int now,
int refresh_unsupported, int *nextcheck, char **error)
{
if (ITEM_STATE_NORMAL == state)
{
int simple_interval;
zbx_custom_interval_t *custom_intervals;
if (SUCCEED != zbx_interval_preproc(delay, &simple_interval, &custom_intervals, error))
{
*nextcheck = ZBX_JAN_2038;
return FAIL;
}
*nextcheck = calculate_item_nextcheck(itemid, ITEM_TYPE_ZABBIX, simple_interval, custom_intervals, now);
zbx_custom_interval_free(custom_intervals);
}
else /* for items notsupported for other reasons use refresh_unsupported interval */
{
*nextcheck = calculate_item_nextcheck(itemid, ITEM_TYPE_ZABBIX, refresh_unsupported, NULL, now);
}
return SUCCEED;
}
|