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 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622
|
/**************************************************************************
*
* ARCHIVEJSON.C - Nagios CGI for returning JSON-formatted archive data
*
* Copyright (c) 2013 Nagios Enterprises, LLC
* Last Modified: 04-13-2013
*
* License:
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*************************************************************************/
#include "../include/config.h"
#include "../include/common.h"
#include "../include/objects.h"
#include "../include/statusdata.h"
#include "../include/cgiutils.h"
#include "../include/getcgi.h"
#include "../include/cgiauth.h"
#include "../include/jsonutils.h"
#include "../include/archiveutils.h"
#include "../include/archivejson.h"
#define THISCGI "archivejson.cgi"
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))
extern char main_config_file[MAX_FILENAME_LENGTH];
extern int log_rotation_method;
extern char *status_file;
extern host *host_list;
extern hostgroup *hostgroup_list;
extern service *service_list;
extern servicegroup *servicegroup_list;
#if 0
extern contact *contact_list;
extern contactgroup *contactgroup_list;
extern timeperiod *timeperiod_list;
extern command *command_list;
extern servicedependency *servicedependency_list;
extern serviceescalation *serviceescalation_list;
extern hostdependency *hostdependency_list;
extern hostescalation *hostescalation_list;
#endif
void document_header();
void document_footer(void);
void init_cgi_data(archive_json_cgi_data *);
int process_cgivars(json_object *, archive_json_cgi_data *, time_t);
void free_cgi_data(archive_json_cgi_data *);
int validate_arguments(json_object *, archive_json_cgi_data *, time_t);
authdata current_authdata;
const string_value_mapping valid_queries[] = {
{ "help", ARCHIVE_QUERY_HELP,
"Display help for this CGI" },
{ "alertcount", ARCHIVE_QUERY_ALERTCOUNT,
"Return the number of alerts" },
{ "alertlist", ARCHIVE_QUERY_ALERTLIST,
"Return a list of alerts" },
{ "notificationcount", ARCHIVE_QUERY_NOTIFICATIONCOUNT,
"Return the number of notifications" },
{ "notificationlist", ARCHIVE_QUERY_NOTIFICATIONLIST,
"Return a list of notifications" },
{ "statechangelist", ARCHIVE_QUERY_STATECHANGELIST,
"Return a list of state changes" },
{ "availability", ARCHIVE_QUERY_AVAILABILITY,
"Return an availability report" },
{ NULL, -1, NULL },
};
static const int query_status[][2] = {
{ ARCHIVE_QUERY_HELP, QUERY_STATUS_RELEASED },
{ ARCHIVE_QUERY_ALERTCOUNT, QUERY_STATUS_RELEASED },
{ ARCHIVE_QUERY_ALERTLIST, QUERY_STATUS_RELEASED },
{ ARCHIVE_QUERY_NOTIFICATIONCOUNT, QUERY_STATUS_RELEASED },
{ ARCHIVE_QUERY_NOTIFICATIONLIST, QUERY_STATUS_RELEASED },
{ ARCHIVE_QUERY_STATECHANGELIST, QUERY_STATUS_RELEASED },
{ ARCHIVE_QUERY_AVAILABILITY, QUERY_STATUS_RELEASED },
{ -1, -1 },
};
const string_value_mapping valid_object_types[] = {
{ "host", AU_OBJTYPE_HOST, "Host" },
{ "service", AU_OBJTYPE_SERVICE, "Service" },
{ NULL, -1, NULL },
};
const string_value_mapping valid_availability_object_types[] = {
{ "hosts", AU_OBJTYPE_HOST, "Hosts" },
{ "hostgroups", AU_OBJTYPE_HOSTGROUP, "Hostgroups" },
{ "services", AU_OBJTYPE_SERVICE, "Services" },
{ "servicegroups", AU_OBJTYPE_SERVICEGROUP, "Servicegroups" },
{ NULL, -1, NULL },
};
const string_value_mapping valid_state_types[] = {
{ "hard", AU_STATETYPE_HARD, "Hard" },
{ "soft", AU_STATETYPE_SOFT, "Soft" },
{ NULL, -1, NULL },
};
const string_value_mapping valid_states[] = {
{ "no_data", AU_STATE_NO_DATA, "No Data" },
{ "host_up", AU_STATE_HOST_UP, "Host Up" },
{ "host_down", AU_STATE_HOST_DOWN, "Host Down" },
{ "host_unreachable", AU_STATE_HOST_UNREACHABLE, "Host Unreachable" },
{ "service_ok", AU_STATE_SERVICE_OK, "Service OK" },
{ "service_warning", AU_STATE_SERVICE_WARNING, "Service Warning" },
{ "service_critical", AU_STATE_SERVICE_CRITICAL, "Service Critical" },
{ "service_unknown", AU_STATE_SERVICE_UNKNOWN, "Service Unknown" },
{ "program_start", AU_STATE_PROGRAM_START, "Program Start" },
{ "program_end", AU_STATE_PROGRAM_END, "Program End" },
{ "downtime_start", AU_STATE_DOWNTIME_START, "Downtime Start" },
{ "downtime_end", AU_STATE_DOWNTIME_END, "Downtime End" },
};
const string_value_mapping valid_host_states[] = {
{ "up", AU_STATE_HOST_UP, "Up" },
{ "down", AU_STATE_HOST_DOWN, "Down" },
{ "unreachable", AU_STATE_HOST_UNREACHABLE, "Unreachable" },
{ NULL, -1, NULL },
};
const string_value_mapping valid_initial_host_states[] = {
{ "up", AU_STATE_HOST_UP, "Up" },
{ "down", AU_STATE_HOST_DOWN, "Down" },
{ "unreachable", AU_STATE_HOST_UNREACHABLE, "Unreachable" },
{ "current", AU_STATE_CURRENT_STATE, "Current State" },
{ NULL, -1, NULL },
};
const string_value_mapping valid_service_states[] = {
{ "ok", AU_STATE_SERVICE_OK, "Ok" },
{ "warning", AU_STATE_SERVICE_WARNING, "Warning" },
{ "critical", AU_STATE_SERVICE_CRITICAL, "Critical" },
{ "unknown", AU_STATE_SERVICE_UNKNOWN, "Unknown" },
{ NULL, -1, NULL },
};
const string_value_mapping valid_initial_service_states[] = {
{ "ok", AU_STATE_SERVICE_OK, "Ok" },
{ "warning", AU_STATE_SERVICE_WARNING, "Warning" },
{ "critical", AU_STATE_SERVICE_CRITICAL, "Critical" },
{ "unknown", AU_STATE_SERVICE_UNKNOWN, "Unknown" },
{ "current", AU_STATE_CURRENT_STATE, "Current State" },
{ NULL, -1, NULL },
};
const string_value_mapping valid_host_notification_types[] = {
{ "nodata", AU_NOTIFICATION_NO_DATA,
"No Data" },
{ "down", AU_NOTIFICATION_HOST_DOWN,
"Host Down" },
{ "unreachable", AU_NOTIFICATION_HOST_UNREACHABLE,
"Host Unreachable" },
{ "recovery", AU_NOTIFICATION_HOST_RECOVERY,
"Host Recovery" },
{ "hostcustom", AU_NOTIFICATION_HOST_CUSTOM,
"Host Custom" },
{ "hostack", AU_NOTIFICATION_HOST_ACK,
"Host Acknowledgement" },
{ "hostflapstart", AU_NOTIFICATION_HOST_FLAPPING_START,
"Host Flapping Start" },
{ "hostflapstop", AU_NOTIFICATION_HOST_FLAPPING_STOP,
"Host Flapping Stop" },
{ NULL, -1, NULL },
};
const string_value_mapping valid_service_notification_types[] = {
{ "nodata", AU_NOTIFICATION_NO_DATA,
"No Data" },
{ "critical", AU_NOTIFICATION_SERVICE_CRITICAL,
"Service Critical" },
{ "warning", AU_NOTIFICATION_SERVICE_WARNING,
"Service Warning" },
{ "recovery", AU_NOTIFICATION_SERVICE_RECOVERY,
"Service Recovery" },
{ "custom", AU_NOTIFICATION_SERVICE_CUSTOM,
"Service Custom" },
{ "serviceack", AU_NOTIFICATION_SERVICE_ACK,
"Service Acknowledgement" },
{ "serviceflapstart", AU_NOTIFICATION_SERVICE_FLAPPING_START,
"Service Flapping Start" },
{ "serviceflapstop", AU_NOTIFICATION_SERVICE_FLAPPING_STOP,
"Service Flapping Stop" },
{ "unknown", AU_NOTIFICATION_SERVICE_UNKNOWN,
"Service Unknown" },
{ NULL, -1, NULL },
};
option_help archive_json_help[] = {
{
"query",
"Query",
"enumeration",
{ "all", NULL },
{ NULL },
NULL,
"Specifies the type of query to be executed.",
valid_queries
},
{
"formatoptions",
"Format Options",
"list",
{ NULL },
{ "all", NULL },
NULL,
"Specifies the formatting options to be used when displaying the results. Multiple options are allowed and are separated by a plus (+) sign..",
svm_format_options
},
{
"start",
"Start",
"integer",
{ NULL },
{ "alertlist", "notificationlist", "statechangelist", NULL },
NULL,
"Specifies the index (zero-based) of the first object in the list to be returned.",
NULL
},
{
"count",
"Count",
"integer",
{ NULL },
{ "alertlist", "notificationlist", "statechangelist", NULL },
NULL,
"Specifies the number of objects in the list to be returned.",
NULL
},
{
"dateformat",
"Date Format",
"string",
{ NULL },
{ "all", NULL },
NULL,
"strftime format string for values of type time_t. In the absence of a format, the Javascript default format of the number of milliseconds since the beginning of the Unix epoch is used. Because of URL encoding, percent signs must be encoded as %25 and a space must be encoded as a plus (+) sign.",
NULL
},
{
"objecttypes",
"Object Types",
"list",
{ NULL },
{ "alertcount", "alertlist", "notificationcount", "notificationlist",
NULL },
NULL,
"Type(s) of object to be included in query results.",
valid_object_types
},
{
"objecttype",
"Object Type",
"enumeration",
{ "statechangelist", NULL },
{ NULL },
NULL,
"Type of object to be included in query results.",
valid_object_types
},
{
"availabilityobjecttype",
"Availability Object Type",
"enumeration",
{ "availability", NULL },
{ NULL },
NULL,
"Type of object to be included in query results.",
valid_availability_object_types
},
{
"statetypes",
"State Types",
"list",
{ NULL },
{ "alertcount", "alertlist", "statechangelist", "availability", NULL },
NULL,
"Type(s) of states to be included in query results.",
valid_state_types
},
{
"hoststates",
"Host States",
"list",
{ NULL },
{ "alertcount", "alertlist", NULL },
NULL,
"Host states to be included in query results.",
valid_host_states
},
{
"servicestates",
"Service States",
"list",
{ NULL },
{ "alertcount", "alertlist", NULL },
NULL,
"Service states to be included in query results.",
valid_service_states
},
{
"hostnotificationtypes",
"Host Notification Types",
"list",
{ NULL },
{ "notificationcount", "notificationlist", NULL },
NULL,
"Host notification types to be included in query results.",
valid_host_notification_types
},
{
"servicenotificationtypes",
"Service Notification Types",
"list",
{ NULL },
{ "notificationcount", "notificationlist", NULL },
NULL,
"Service notification types to be included in query results.",
valid_service_notification_types
},
{
"parenthost",
"Parent Host",
"nagios:objectjson/hostlist",
{ NULL },
{ "alertcount", "alertlist", "notificationcount", "notificationlist",
NULL },
NULL,
"Limits the hosts or services returned to those whose host parent is specified. A value of 'none' returns all hosts or services reachable directly by the Nagios core host.",
parent_host_extras
},
{
"childhost",
"Child Host",
"nagios:objectjson/hostlist",
{ NULL },
{ "alertcount", "alertlist", "notificationcount", "notificationlist",
NULL },
NULL,
"Limits the hosts or services returned to those whose having the host specified as a child host. A value of 'none' returns all hosts or services with no child hosts.",
child_host_extras
},
{
"hostname",
"Host Name",
"nagios:objectjson/hostlist",
{ "statechangelist", NULL },
{ "alertcount", "alertlist", "notificationcount", "notificationlist",
"availability", NULL },
NULL,
"Name for the host requested. For availability reports if the "
"availability object type is hosts and the hostname is not "
"specified, the report will be generated for all hosts. Likewise, "
"if the availability object type is services and the hostname is not "
"specified, the report will be generated for all services or all "
"services with the same description, depending on the value for "
"service description.",
NULL
},
{
"hostgroup",
"Host Group",
"nagios:objectjson/hostgrouplist",
{ NULL },
{ "alertcount", "alertlist", "notificationcount", "notificationlist",
"availability", NULL },
NULL,
"Returns information applicable to the hosts in the hostgroup.",
NULL
},
{
"servicegroup",
"Service Group",
"nagios:objectjson/servicegrouplist",
{ NULL },
{ "alertcount", "alertlist", "notificationcount", "notificationlist",
"availability", NULL },
NULL,
"Returns information applicable to the services in the servicegroup.",
NULL
},
{
"servicedescription",
"Service Description",
"nagios:objectjson/servicelist",
{ NULL },
{ "alertcount", "alertlist", "notificationcount", "notificationlist",
"statechangelist", "availability", NULL },
"hostname",
"Description for the service requested. For availability reports, "
"if the availability object type is services and the "
"servicedescription is not specified, the report will be generated "
"either for all services or for all services on the specified host, "
"depending on the value specified for hostname",
NULL
},
{
"contactname",
"Contact Name",
"nagios:objectjson/contactlist",
{ NULL },
{ "alertcount", "alertlist", "notificationcount", "notificationlist",
NULL },
NULL,
"Name for the contact requested.",
NULL
},
{
"contactgroup",
"Contact Group",
"nagios:objectjson/contactgrouplist",
{ NULL },
{ "alertcount", "alertlist", "notificationcount", "notificationlist",
NULL },
NULL,
"Returns information applicable to the contacts in the contactgroup.",
NULL
},
{
"notificationmethod",
"Notification Method",
"nagios:objectjson/commandlist",
{ NULL },
{ "notificationcount", "notificationlist", NULL },
NULL,
"Returns objects that match the notification method.",
NULL
},
{
"timeperiod",
"Report Time Period",
"nagios:objectjson/timeperiodlist",
{ NULL },
{ "availability", NULL },
NULL,
"Timeperiod to use for the report.",
NULL
},
{
"assumeinitialstate",
"Assume Initial State",
"boolean",
{ NULL },
{ "availability", NULL },
NULL,
"Assume the initial state for the host(s) or service(s). Note that if true, assuming the initial state will be done only if the initial state could not be discovered by looking through the backtracked logs.",
NULL
},
{
"assumestateretention",
"Assume State Retention",
"boolean",
{ NULL },
{ "availability", NULL },
NULL,
"Assume states are retained.",
NULL
},
{
"assumestatesduringnagiosdowntime",
"Assume States During Nagios Downtime",
"boolean",
{ NULL },
{ "availability", NULL },
NULL,
"Assume states are retained during Nagios downtime.",
NULL
},
{
"assumedinitialhoststate",
"Assumed Initial Host State",
"enumeration",
{ NULL },
{ "availability", NULL },
NULL,
"Host state assumed when it is not possible to determine the initial host state.",
valid_initial_host_states
},
{
"assumedinitialservicestate",
"Assumed Initial Service State",
"enumeration",
{ NULL },
{ "availability", NULL },
NULL,
"Service state assumed when it is not possible to determine the initial service state.",
valid_initial_service_states
},
{
"backtrackedarchives",
"Backtracked Archives",
"integer",
{ NULL },
{ "alertcount", "alertlist", "notificationcount", "notificationlist",
"statechangelist", "availability", NULL },
NULL,
"Number of backtracked archives to read in an attempt to find initial states.",
NULL,
},
{
"starttime",
"Start Time",
"integer",
{ "alertcount", "alertlist", "notificationcount", "notificationlist",
"statechangelist", "availability", NULL },
{ NULL },
NULL,
"Starting time to use. Supplying a plus or minus sign means times relative to the query time.",
NULL,
},
{
"endtime",
"End Time",
"integer",
{ "alertcount", "alertlist", "notificationcount", "notificationlist",
"statechangelist", "availability", NULL },
{ NULL },
NULL,
"Ending time to use. Specifying plus or minus sign means times relative to the query time.",
NULL,
},
{ /* The last entry must contain all NULL entries */
NULL,
NULL,
NULL,
{ NULL },
{ NULL },
NULL,
NULL,
NULL
},
};
extern const json_escape percent_escapes;
int json_archive_alert_passes_selection(time_t, time_t, time_t, int, int,
au_host *, char *, au_service *, char *, int, host *, int, host *,
hostgroup *, servicegroup *, contact *, contactgroup *, unsigned,
unsigned, unsigned, unsigned, unsigned);
json_object *json_archive_alert_selectors(unsigned, int, int, time_t, time_t,
int, char *, char *, int, host *, int, host *, hostgroup *,
servicegroup *, contact *, contactgroup *, unsigned, unsigned,
unsigned);
int json_archive_notification_passes_selection(time_t, time_t, time_t, int,
int, au_host *, char *, au_service *, char *, int, host *, int, host *,
hostgroup *, servicegroup *, au_contact *, char *, contactgroup *,
unsigned, unsigned, unsigned, char *, char *);
json_object *json_archive_notification_selectors(unsigned, int, int, time_t,
time_t, int, char *, char *, int, host *, int, host *, hostgroup *,
servicegroup *, char *, contactgroup *, unsigned, unsigned, char *);
int json_archive_statechange_passes_selection(time_t, time_t, int, int,
au_host *, char *, au_service *, char *, unsigned, unsigned);
json_object *json_archive_statechange_selectors(unsigned, int, int, time_t,
time_t, int, char *, char *, unsigned);
int get_initial_nagios_state(au_linked_list *, time_t, time_t);
int get_initial_downtime_state(au_linked_list *, time_t, time_t);
int get_initial_subject_state(au_linked_list *, time_t, time_t);
int get_log_entry_state(au_log_entry *);
int have_data(au_linked_list *, int);
void compute_window_availability(time_t, time_t, int, int, int, timeperiod *,
au_availability *, int, int, int);
static void compute_availability(au_linked_list *, time_t, time_t, time_t,
timeperiod *, au_availability *, int, int, int, int);
void compute_host_availability(time_t, time_t, time_t, au_log *, au_host *,
timeperiod *, int, int, int, int);
void compute_service_availability(time_t, time_t, time_t, au_log *,
au_service *, timeperiod *, int, int, int, int);
json_object *json_archive_single_host_availability(unsigned, time_t, time_t,
time_t, char *, timeperiod *, int, int, int, int, unsigned, au_log *);
json_object *json_archive_single_service_availability(unsigned, time_t, time_t,
time_t, char *, char *, timeperiod *, int, int, int, int, unsigned,
au_log *);
json_object *json_archive_host_availability(unsigned, char *,
au_availability *);
json_object *json_archive_service_availability(unsigned, char *, char *,
au_availability *);
int main(void) {
int result = OK;
time_t query_time;
archive_json_cgi_data cgi_data;
json_object *json_root;
au_log *log;
time_t last_archive_data_update = (time_t)0;
json_object_member *romp = NULL;
/* The official time of the query */
time(&query_time);
json_root = json_new_object();
if(NULL == json_root) {
printf( "Failed to create new json object\n");
exit( 1);
}
json_object_append_integer(json_root, "format_version",
OUTPUT_FORMAT_VERSION);
/* Initialize shared configuration variables */
init_shared_cfg_vars(1);
init_cgi_data(&cgi_data);
document_header();
/* get the arguments passed in the URL */
result = process_cgivars(json_root, &cgi_data, query_time);
if(result != RESULT_SUCCESS) {
json_object_append_object(json_root, "data",
json_help(archive_json_help));
json_object_print(json_root, 0, 1, cgi_data.strftime_format,
cgi_data.format_options);
document_footer();
return result;
}
/* reset internal variables */
reset_cgi_vars();
/* read the CGI configuration file */
result = read_cgi_config_file(get_cgi_config_location(), NULL);
if(result == ERROR) {
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data.query, valid_queries),
get_query_status(query_status, cgi_data.query),
(time_t)-1, NULL, RESULT_FILE_OPEN_READ_ERROR,
"Error: Could not open CGI configuration file '%s' for reading!",
get_cgi_config_location()));
json_object_append_object(json_root, "data",
json_help(archive_json_help));
json_object_print(json_root, 0, 1, cgi_data.strftime_format,
cgi_data.format_options);
document_footer();
return ERROR;
}
/* read the main configuration file */
result = read_main_config_file(main_config_file);
if(result == ERROR) {
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data.query, valid_queries),
get_query_status(query_status, cgi_data.query),
(time_t)-1, NULL, RESULT_FILE_OPEN_READ_ERROR,
"Error: Could not open main configuration file '%s' for reading!",
main_config_file));
json_object_append_object(json_root, "data",
json_help(archive_json_help));
document_footer();
return ERROR;
}
/* Set the number of backtracked archives if it wasn't specified by the
user */
if(0 == cgi_data.backtracked_archives) {
switch(log_rotation_method) {
case LOG_ROTATION_MONTHLY:
cgi_data.backtracked_archives = 1;
break;
case LOG_ROTATION_WEEKLY:
cgi_data.backtracked_archives = 2;
break;
case LOG_ROTATION_DAILY:
cgi_data.backtracked_archives = 4;
break;
case LOG_ROTATION_HOURLY:
cgi_data.backtracked_archives = 8;
break;
default:
cgi_data.backtracked_archives = 2;
break;
}
}
/* read all object configuration data */
result = read_all_object_configuration_data(main_config_file,
READ_ALL_OBJECT_DATA);
if(result == ERROR) {
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data.query, valid_queries),
get_query_status(query_status, cgi_data.query),
(time_t)-1, NULL, RESULT_FILE_OPEN_READ_ERROR,
"Error: Could not read some or all object configuration data!"));
json_object_append_object(json_root, "data",
json_help(archive_json_help));
document_footer();
return ERROR;
}
/* read all status data */
result = read_all_status_data(status_file, READ_ALL_STATUS_DATA);
if(result == ERROR) {
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data.query, valid_queries),
get_query_status(query_status, cgi_data.query),
(time_t)-1, NULL, RESULT_FILE_OPEN_READ_ERROR,
"Error: Could not read host and service status information!"));
json_object_append_object(json_root, "data",
json_help(archive_json_help));
document_footer();
return ERROR;
}
/* validate arguments in URL */
result = validate_arguments(json_root, &cgi_data, query_time);
if((result != RESULT_SUCCESS) && (result != RESULT_OPTION_IGNORED)) {
json_object_append_object(json_root, "data",
json_help(archive_json_help));
json_object_print(json_root, 0, 1, cgi_data.strftime_format,
cgi_data.format_options);
document_footer();
return ERROR;
}
/* get authentication information */
get_authentication_information(¤t_authdata);
/* Allocate the structure for the logs */
if((log = au_init_log()) == NULL) {
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data.query, valid_queries),
get_query_status(query_status, cgi_data.query),
(time_t)-1, ¤t_authdata, RESULT_MEMORY_ALLOCATION_ERROR,
"Unable to allocate memory for log data."));
json_object_append_object(json_root, "data",
json_help(archive_json_help));
document_footer();
return ERROR;
}
/* Return something to the user */
switch( cgi_data.query) {
case ARCHIVE_QUERY_HELP:
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data.query, valid_queries),
get_query_status(query_status, cgi_data.query),
compile_time(__DATE__, __TIME__), ¤t_authdata,
RESULT_SUCCESS, ""));
json_object_append_object(json_root, "data",
json_help(archive_json_help));
break;
case ARCHIVE_QUERY_ALERTCOUNT:
read_archived_data(cgi_data.start_time, cgi_data.end_time,
cgi_data.backtracked_archives, AU_OBJTYPE_ALL,
AU_STATETYPE_ALL, AU_LOGTYPE_ALERT | AU_LOGTYPE_STATE, log,
&last_archive_data_update);
if(result != RESULT_OPTION_IGNORED) {
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data.query, valid_queries),
get_query_status(query_status, cgi_data.query),
last_archive_data_update, ¤t_authdata,
RESULT_SUCCESS, ""));
}
else {
romp = json_get_object_member(json_root, "result");
json_object_append_time_t(romp->value.object, "last_data_update",
last_archive_data_update);
}
json_object_append_object(json_root, "data",
json_archive_alertcount(cgi_data.format_options,
cgi_data.start_time, cgi_data.end_time, cgi_data.object_types,
cgi_data.host_name, cgi_data.service_description,
cgi_data.use_parent_host, cgi_data.parent_host,
cgi_data.use_child_host, cgi_data.child_host,
cgi_data.hostgroup, cgi_data.servicegroup, cgi_data.contact,
cgi_data.contactgroup, cgi_data.state_types,
cgi_data.host_states, cgi_data.service_states, log));
break;
case ARCHIVE_QUERY_ALERTLIST:
read_archived_data(cgi_data.start_time, cgi_data.end_time,
cgi_data.backtracked_archives, AU_OBJTYPE_ALL,
AU_STATETYPE_ALL, AU_LOGTYPE_ALERT | AU_LOGTYPE_STATE, log,
&last_archive_data_update);
if(result != RESULT_OPTION_IGNORED) {
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data.query, valid_queries),
get_query_status(query_status, cgi_data.query),
last_archive_data_update, ¤t_authdata,
RESULT_SUCCESS, ""));
}
else {
romp = json_get_object_member(json_root, "result");
json_object_append_time_t(romp->value.object, "last_data_update",
last_archive_data_update);
}
json_object_append_object(json_root, "data",
json_archive_alertlist(cgi_data.format_options, cgi_data.start,
cgi_data.count, cgi_data.start_time, cgi_data.end_time,
cgi_data.object_types, cgi_data.host_name,
cgi_data.service_description, cgi_data.use_parent_host,
cgi_data.parent_host, cgi_data.use_child_host,
cgi_data.child_host, cgi_data.hostgroup, cgi_data.servicegroup,
cgi_data.contact, cgi_data.contactgroup, cgi_data.state_types,
cgi_data.host_states, cgi_data.service_states, log));
break;
case ARCHIVE_QUERY_NOTIFICATIONCOUNT:
read_archived_data(cgi_data.start_time, cgi_data.end_time,
cgi_data.backtracked_archives, AU_OBJTYPE_ALL,
AU_STATETYPE_ALL, AU_LOGTYPE_NOTIFICATION, log,
&last_archive_data_update);
if(result != RESULT_OPTION_IGNORED) {
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data.query, valid_queries),
get_query_status(query_status, cgi_data.query),
last_archive_data_update, ¤t_authdata,
RESULT_SUCCESS, ""));
}
else {
romp = json_get_object_member(json_root, "result");
json_object_append_time_t(romp->value.object, "last_data_update",
last_archive_data_update);
}
json_object_append_object(json_root, "data",
json_archive_notificationcount(cgi_data.format_options,
cgi_data.start_time, cgi_data.end_time, cgi_data.object_types,
cgi_data.host_name, cgi_data.service_description,
cgi_data.use_parent_host, cgi_data.parent_host,
cgi_data.use_child_host, cgi_data.child_host,
cgi_data.hostgroup, cgi_data.servicegroup,
cgi_data.contact_name, cgi_data.contactgroup,
cgi_data.host_notification_types,
cgi_data.service_notification_types,
cgi_data.notification_method, log));
break;
case ARCHIVE_QUERY_NOTIFICATIONLIST:
read_archived_data(cgi_data.start_time, cgi_data.end_time,
cgi_data.backtracked_archives, AU_OBJTYPE_ALL,
AU_STATETYPE_ALL, AU_LOGTYPE_NOTIFICATION, log,
&last_archive_data_update);
if(result != RESULT_OPTION_IGNORED) {
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data.query, valid_queries),
get_query_status(query_status, cgi_data.query),
last_archive_data_update, ¤t_authdata,
RESULT_SUCCESS, ""));
}
else {
romp = json_get_object_member(json_root, "result");
json_object_append_time_t(romp->value.object, "last_data_update",
last_archive_data_update);
}
json_object_append_object(json_root, "data",
json_archive_notificationlist(cgi_data.format_options,
cgi_data.start, cgi_data.count, cgi_data.start_time,
cgi_data.end_time, cgi_data.object_types, cgi_data.host_name,
cgi_data.service_description, cgi_data.use_parent_host,
cgi_data.parent_host, cgi_data.use_child_host,
cgi_data.child_host, cgi_data.hostgroup, cgi_data.servicegroup,
cgi_data.contact_name, cgi_data.contactgroup,
cgi_data.host_notification_types,
cgi_data.service_notification_types,
cgi_data.notification_method, log));
break;
case ARCHIVE_QUERY_STATECHANGELIST:
read_archived_data(cgi_data.start_time, cgi_data.end_time,
cgi_data.backtracked_archives, cgi_data.object_type,
AU_STATETYPE_ALL, (AU_LOGTYPE_ALERT | AU_LOGTYPE_STATE), log,
&last_archive_data_update);
if(result != RESULT_OPTION_IGNORED) {
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data.query, valid_queries),
get_query_status(query_status, cgi_data.query),
last_archive_data_update, ¤t_authdata,
RESULT_SUCCESS, ""));
}
else {
romp = json_get_object_member(json_root, "result");
json_object_append_time_t(romp->value.object, "last_data_update",
last_archive_data_update);
}
json_object_append_object(json_root, "data",
json_archive_statechangelist(cgi_data.format_options,
cgi_data.start, cgi_data.count, cgi_data.start_time,
cgi_data.end_time, cgi_data.object_type, cgi_data.host_name,
cgi_data.service_description,
cgi_data.assumed_initial_host_state,
cgi_data.assumed_initial_service_state, cgi_data.state_types,
log));
break;
case ARCHIVE_QUERY_AVAILABILITY:
switch(cgi_data.object_type) {
case AU_OBJTYPE_HOST:
case AU_OBJTYPE_HOSTGROUP:
read_archived_data(cgi_data.start_time, cgi_data.end_time,
cgi_data.backtracked_archives, AU_OBJTYPE_HOST,
cgi_data.state_types, (AU_LOGTYPE_NAGIOS | AU_LOGTYPE_ALERT
| AU_LOGTYPE_STATE | AU_LOGTYPE_DOWNTIME), log,
&last_archive_data_update);
break;
case AU_OBJTYPE_SERVICE:
case AU_OBJTYPE_SERVICEGROUP:
read_archived_data(cgi_data.start_time, cgi_data.end_time,
cgi_data.backtracked_archives, AU_OBJTYPE_SERVICE,
cgi_data.state_types, (AU_LOGTYPE_NAGIOS | AU_LOGTYPE_ALERT
| AU_LOGTYPE_STATE | AU_LOGTYPE_DOWNTIME), log,
&last_archive_data_update);
break;
}
if(result != RESULT_OPTION_IGNORED) {
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data.query, valid_queries),
get_query_status(query_status, cgi_data.query),
last_archive_data_update, ¤t_authdata,
RESULT_SUCCESS, ""));
}
else {
romp = json_get_object_member(json_root, "result");
json_object_append_time_t(romp->value.object, "last_data_update",
last_archive_data_update);
}
json_object_append_object(json_root, "data",
json_archive_availability(cgi_data.format_options, query_time,
cgi_data.start_time, cgi_data.end_time,
cgi_data.object_type, cgi_data.host_name,
cgi_data.service_description, cgi_data.hostgroup,
cgi_data.servicegroup, cgi_data.timeperiod,
cgi_data.assume_initial_state, cgi_data.assume_state_retention,
cgi_data.assume_states_during_nagios_downtime,
cgi_data.assumed_initial_host_state,
cgi_data.assumed_initial_service_state, cgi_data.state_types,
log));
break;
default:
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data.query, valid_queries),
get_query_status(query_status, cgi_data.query),
(time_t)-1, ¤t_authdata, RESULT_OPTION_MISSING,
"Error: Object Type not specified. See data for help."));
json_object_append_object(json_root, "data",
json_help(archive_json_help));
break;
}
json_object_print(json_root, 0, 1, cgi_data.strftime_format,
cgi_data.format_options);
document_footer();
/* free all allocated memory */
free_cgi_data( &cgi_data);
json_free_object(json_root, 1);
au_free_log(log);
free_memory();
return OK;
}
void document_header() {
char date_time[MAX_DATETIME_LENGTH];
time_t expire_time;
time_t current_time;
time(¤t_time);
printf("Cache-Control: no-store\r\n");
printf("Pragma: no-cache\r\n");
get_time_string(¤t_time, date_time, (int)sizeof(date_time), HTTP_DATE_TIME);
printf("Last-Modified: %s\r\n", date_time);
expire_time = (time_t)0L;
get_time_string(&expire_time, date_time, (int)sizeof(date_time), HTTP_DATE_TIME);
printf("Expires: %s\r\n", date_time);
printf("Content-type: application/json; charset=utf-8\r\n\r\n");
return;
}
void document_footer(void) {
printf( "\n");
return;
}
void init_cgi_data(archive_json_cgi_data *cgi_data) {
cgi_data->format_options = 0;
cgi_data->query = ARCHIVE_QUERY_INVALID;
cgi_data->start = 0;
cgi_data->count = 0;
cgi_data->object_types = AU_OBJTYPE_ALL;
cgi_data->object_type = 0;
cgi_data->state_types = AU_STATETYPE_ALL;
cgi_data->host_states = AU_STATE_HOST_ALL;
cgi_data->service_states = AU_STATE_SERVICE_ALL;
cgi_data->host_notification_types = AU_NOTIFICATION_HOST_ALL;
cgi_data->service_notification_types = AU_NOTIFICATION_SERVICE_ALL;
#if 0
cgi_data->details = 0;
#endif
cgi_data->strftime_format = NULL;
cgi_data->start_time = (time_t)0;
cgi_data->end_time = (time_t)0;
cgi_data->parent_host_name = NULL;
cgi_data->use_parent_host = 0;
cgi_data->parent_host = NULL;
cgi_data->child_host_name = NULL;
cgi_data->use_child_host = 0;
cgi_data->child_host = NULL;
cgi_data->host_name = NULL;
cgi_data->host = NULL;
cgi_data->hostgroup_name = NULL;
cgi_data->hostgroup = NULL;
cgi_data->servicegroup_name = NULL;
cgi_data->servicegroup = NULL;
cgi_data->service_description = NULL;
cgi_data->service = NULL;
cgi_data->contact_name = NULL;
cgi_data->contact = NULL;
cgi_data->contactgroup_name = NULL;
cgi_data->contactgroup = NULL;
cgi_data->notification_method = NULL;
cgi_data->timeperiod_name = NULL;
cgi_data->timeperiod = NULL;
cgi_data->assumed_initial_host_state = AU_STATE_NO_DATA;
cgi_data->assumed_initial_service_state = AU_STATE_NO_DATA;
cgi_data->assume_initial_state = 1;
cgi_data->assume_state_retention = 1;
cgi_data->assume_states_during_nagios_downtime = 1;
cgi_data->backtracked_archives = 0;
}
void free_cgi_data(archive_json_cgi_data *cgi_data) {
if(NULL != cgi_data->strftime_format) free( cgi_data->strftime_format);
if(NULL != cgi_data->parent_host_name) free( cgi_data->parent_host_name);
if(NULL != cgi_data->child_host_name) free( cgi_data->child_host_name);
if(NULL != cgi_data->host_name) free( cgi_data->host_name);
if(NULL != cgi_data->hostgroup_name) free( cgi_data->hostgroup_name);
if(NULL != cgi_data->servicegroup_name) free(cgi_data->servicegroup_name);
if(NULL != cgi_data->service_description) free(cgi_data->service_description);
if(NULL != cgi_data->contact_name) free(cgi_data->contact_name);
if(NULL != cgi_data->contactgroup_name) free(cgi_data->contactgroup_name);
if(NULL != cgi_data->notification_method) free(cgi_data->notification_method);
if(NULL != cgi_data->timeperiod_name) free(cgi_data->timeperiod_name);
}
int process_cgivars(json_object *json_root, archive_json_cgi_data *cgi_data,
time_t query_time) {
char **variables;
int result = RESULT_SUCCESS;
int x;
authdata *authinfo = NULL; /* Currently always NULL because
get_authentication_information() hasn't
been called yet, but in case we want to
use it in the future... */
variables = getcgivars();
for(x = 0; variables[x]; x++) {
/* We set these each iteration because they could change with each
iteration */
if(!strcmp(variables[x], "query")) {
if((result = parse_enumeration_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], valid_queries, &(cgi_data->query)))
!= RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "formatoptions")) {
cgi_data->format_options = 0;
if((result = parse_bitmask_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], svm_format_options,
&(cgi_data->format_options))) != RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "backtrackedarchives")) {
if((result = parse_int_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], &(cgi_data->backtracked_archives)))
!= RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "start")) {
if((result = parse_int_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], &(cgi_data->start))) != RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "count")) {
if((result = parse_int_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], &(cgi_data->count))) != RESULT_SUCCESS) {
break;
}
if(cgi_data->count == 0) {
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query,
valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, RESULT_OPTION_VALUE_INVALID,
"The count option value is invalid. "
"It must be an integer greater than zero"));
result = RESULT_OPTION_VALUE_INVALID;
break;
}
x++;
}
else if(!strcmp(variables[x], "objecttype")) {
if((result = parse_enumeration_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], valid_object_types,
&(cgi_data->object_type))) != RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "objecttypes")) {
cgi_data->object_types = 0;
if((result = parse_bitmask_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], valid_object_types,
&(cgi_data->object_types))) != RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "availabilityobjecttype")) {
if((result = parse_enumeration_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], valid_availability_object_types,
&(cgi_data->object_type))) != RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "statetypes")) {
cgi_data->state_types = 0;
if((result = parse_bitmask_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], valid_state_types,
&(cgi_data->state_types))) != RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "hoststates")) {
cgi_data->host_states = 0;
if((result = parse_bitmask_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], valid_host_states,
&(cgi_data->host_states))) != RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "servicestates")) {
cgi_data->service_states = 0;
if((result = parse_bitmask_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], valid_service_states,
&(cgi_data->service_states))) != RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "hostnotificationtypes")) {
cgi_data->host_notification_types = 0;
if((result = parse_bitmask_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], valid_host_notification_types,
&(cgi_data->host_notification_types))) != RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "servicenotificationtypes")) {
cgi_data->service_notification_types = 0;
if((result = parse_bitmask_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], valid_service_notification_types,
&(cgi_data->service_notification_types)))
!= RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "parenthost")) {
if((result = parse_string_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], &(cgi_data->parent_host_name)))
!= RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "childhost")) {
if((result = parse_string_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], &(cgi_data->child_host_name)))
!= RESULT_SUCCESS) {
}
x++;
}
else if(!strcmp(variables[x], "hostname")) {
if((result = parse_string_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], &(cgi_data->host_name)))
!= RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "hostgroup")) {
if((result = parse_string_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], &(cgi_data->hostgroup_name)))
!= RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "servicegroup")) {
if((result = parse_string_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], &(cgi_data->servicegroup_name)))
!= RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "servicedescription")) {
if((result = parse_string_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], &(cgi_data->service_description)))
!= RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "contactname")) {
if((result = parse_string_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], &(cgi_data->contact_name)))
!= RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "contactgroup")) {
if((result = parse_string_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], &(cgi_data->contactgroup_name)))
!= RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "notificationmethod")) {
if((result = parse_string_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], &(cgi_data->notification_method)))
!= RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "timeperiod")) {
if((result = parse_string_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], &(cgi_data->timeperiod_name)))
!= RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "assumeinitialstate")) {
if((result = parse_boolean_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], &(cgi_data->assume_initial_state)))
!= RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "assumestateretention")) {
if((result = parse_boolean_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], &(cgi_data->assume_state_retention)))
!= RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "assumestatesduringnagiosdowntime")) {
if((result = parse_boolean_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1],
&(cgi_data->assume_states_during_nagios_downtime)))
!= RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "assumedinitialhoststate")) {
if((result = parse_enumeration_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], valid_initial_host_states,
&(cgi_data->assumed_initial_host_state)))
!= RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "assumedinitialservicestate")) {
if((result = parse_enumeration_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], valid_initial_service_states,
&(cgi_data->assumed_initial_service_state)))
!= RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "dateformat")) {
if((result = parse_string_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], &(cgi_data->strftime_format)))
!= RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "starttime")) {
if((result = parse_time_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], &(cgi_data->start_time)))
!= RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "endtime")) {
if((result = parse_time_cgivar(THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
json_root, query_time, authinfo, variables[x],
variables[x+1], &(cgi_data->end_time))) != RESULT_SUCCESS) {
break;
}
x++;
}
else if(!strcmp(variables[x], "NagFormId"))
++x;
else if(!strcmp(variables[x], ""));
else {
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, RESULT_OPTION_INVALID,
"Invalid option: '%s'.", variables[x]));
result = RESULT_OPTION_INVALID;
break;
}
}
/* free memory allocated to the CGI variables */
free_cgivars(variables);
return result;
}
int validate_arguments(json_object *json_root, archive_json_cgi_data *cgi_data,
time_t query_time) {
int result = RESULT_SUCCESS;
host *temp_host = NULL;
hostgroup *temp_hostgroup = NULL;
servicegroup *temp_servicegroup = NULL;
contactgroup *temp_contactgroup = NULL;
timeperiod *temp_timeperiod = NULL;
#if 0
service *temp_service = NULL;
contact *temp_contact = NULL;
command *temp_command = NULL;
#endif
authdata *authinfo = NULL; /* Currently always NULL because
get_authentication_information() hasn't
been called yet, but in case we want to
use it in the future... */
/* Validate that required parameters were supplied */
switch(cgi_data->query) {
case ARCHIVE_QUERY_HELP:
break;
case ARCHIVE_QUERY_ALERTCOUNT:
case ARCHIVE_QUERY_ALERTLIST:
if((0 == cgi_data->start_time) && (0 == cgi_data->end_time)) {
result = RESULT_OPTION_MISSING;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"Start and/or end time must be supplied."));
}
if(!(cgi_data->object_types & (AU_OBJTYPE_HOST | AU_OBJTYPE_SERVICE))) {
result = RESULT_OPTION_MISSING;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"At least one object type must be supplied."));
}
break;
case ARCHIVE_QUERY_NOTIFICATIONCOUNT:
case ARCHIVE_QUERY_NOTIFICATIONLIST:
if((0 == cgi_data->start_time) && (0 == cgi_data->end_time)) {
result = RESULT_OPTION_MISSING;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"Start and/or end time must be supplied."));
}
if(!(cgi_data->object_types & (AU_OBJTYPE_HOST | AU_OBJTYPE_SERVICE))) {
result = RESULT_OPTION_MISSING;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"At least one object type must be supplied."));
}
break;
case ARCHIVE_QUERY_STATECHANGELIST:
if((0 == cgi_data->start_time) && (0 == cgi_data->end_time)) {
result = RESULT_OPTION_MISSING;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"Start and/or end time must be supplied."));
}
if(0 == cgi_data->object_type) {
result = RESULT_OPTION_MISSING;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"Object type must be supplied."));
}
if(NULL == cgi_data->host_name) {
result = RESULT_OPTION_MISSING;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"Host name must be supplied."));
}
if((AU_OBJTYPE_SERVICE == cgi_data->object_type) &&
(NULL == cgi_data->service_description)) {
result = RESULT_OPTION_MISSING;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"Service description must be supplied."));
}
break;
case ARCHIVE_QUERY_AVAILABILITY:
if((0 == cgi_data->start_time) && (0 == cgi_data->end_time)) {
result = RESULT_OPTION_MISSING;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"Start and/or end time must be supplied."));
}
if(0 == cgi_data->object_type) {
result = RESULT_OPTION_MISSING;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"Availability object type must be supplied."));
}
break;
default:
result = RESULT_OPTION_MISSING;
json_object_append_object(json_root, "result", json_result(query_time,
THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"Missing validation for object type %u.", cgi_data->query));
break;
}
/* Attempt to find the host object associated with host_name. Because
we're looking at historical data, the host may no longer
exist. */
if(NULL != cgi_data->host_name) {
cgi_data->host = find_host(cgi_data->host_name);
}
/* Attempt to find the service object associated with host_name and
service_description. Because we're looking at historical data,
the service may no longer exist. */
if((NULL != cgi_data->host_name) &&
(NULL != cgi_data->service_description)) {
cgi_data->service = find_service(cgi_data->host_name,
cgi_data->service_description);
}
/* Validate the requested parent host */
if( NULL != cgi_data->parent_host_name) {
if(strcmp(cgi_data->parent_host_name, "none")) {
temp_host = find_host(cgi_data->parent_host_name);
if( NULL == temp_host) {
result = RESULT_OPTION_VALUE_INVALID;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query,
valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"The parenthost '%s' could not be found.",
cgi_data->parent_host_name));
}
else {
cgi_data->use_parent_host = 1;
cgi_data->parent_host = temp_host;
}
}
else {
cgi_data->use_parent_host = 1;
cgi_data->parent_host = NULL;
}
}
/* Validate the requested child host */
if( NULL != cgi_data->child_host_name) {
if(strcmp(cgi_data->child_host_name, "none")) {
temp_host = find_host(cgi_data->child_host_name);
if( NULL == temp_host) {
result = RESULT_OPTION_VALUE_INVALID;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query,
valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"The childhost '%s' could not be found.",
cgi_data->child_host_name));
}
else {
cgi_data->use_child_host = 1;
cgi_data->child_host = temp_host;
}
}
else {
cgi_data->use_child_host = 1;
cgi_data->child_host = NULL;
}
}
/* Validate the requested hostgroup */
if( NULL != cgi_data->hostgroup_name) {
temp_hostgroup = find_hostgroup(cgi_data->hostgroup_name);
if( NULL == temp_hostgroup) {
result = RESULT_OPTION_VALUE_INVALID;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"The hostgroup '%s' could not be found.",
cgi_data->hostgroup_name));
}
else {
cgi_data->hostgroup = temp_hostgroup;
}
}
/* Validate the requested servicegroup */
if( NULL != cgi_data->servicegroup_name) {
temp_servicegroup = find_servicegroup(cgi_data->servicegroup_name);
if( NULL == temp_servicegroup) {
result = RESULT_OPTION_VALUE_INVALID;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"The servicegroup '%s' could not be found.",
cgi_data->servicegroup_name));
}
else {
cgi_data->servicegroup = temp_servicegroup;
}
}
/* Attempt to find the contact object associated with contact_name.
Because we're looking at historical data, the contact may
no longer exist. */
if(NULL != cgi_data->contact_name) {
cgi_data->contact = find_contact(cgi_data->contact_name);
}
/* Validate the requested contactgroup */
if( NULL != cgi_data->contactgroup_name) {
temp_contactgroup = find_contactgroup(cgi_data->contactgroup_name);
if( NULL == temp_contactgroup) {
result = RESULT_OPTION_VALUE_INVALID;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"The contactgroup '%s' could not be found.",
cgi_data->contactgroup_name));
}
else {
cgi_data->contactgroup = temp_contactgroup;
}
}
/* Validate the requested timeperiod */
if( NULL != cgi_data->timeperiod_name) {
temp_timeperiod = find_timeperiod(cgi_data->timeperiod_name);
if( NULL == temp_timeperiod) {
result = RESULT_OPTION_VALUE_INVALID;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"The timeperiod '%s' could not be found.",
cgi_data->timeperiod_name));
}
else {
cgi_data->timeperiod = temp_timeperiod;
}
}
/* Validate the requested start time is before the requested end time */
if((cgi_data->start_time > 0) && (cgi_data->end_time > 0) && (
cgi_data->start_time >= cgi_data->end_time)) {
result = RESULT_OPTION_VALUE_INVALID;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"The requested start time must be before the end time."));
}
/* If one or more host states were selected, but host objects were not,
notify the user, but continue */
if((cgi_data->host_states != AU_STATE_HOST_ALL) &&
(!(cgi_data->object_types & AU_OBJTYPE_HOST))) {
result = RESULT_OPTION_IGNORED;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result, "The requested host states "
"were ignored because host objects were not selected."));
}
/* If one or more service states were selected, but service objects
were not, notify the user but continue */
else if((cgi_data->service_states != AU_STATE_SERVICE_ALL) &&
(!(cgi_data->object_types & AU_OBJTYPE_SERVICE))) {
result = RESULT_OPTION_IGNORED;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result, "The requested service states "
"were ignored because service objects were not selected."));
}
/* If one or more host notification types were selected, but host
objects were not, notify the user, but continue */
if((cgi_data->host_notification_types != AU_NOTIFICATION_HOST_ALL) &&
(!(cgi_data->object_types & AU_OBJTYPE_HOST))) {
result = RESULT_OPTION_IGNORED;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"The requested host notification types were ignored because host objects were not selected."));
}
/* If one or more service notification types were selected, but
service objects were not, notify the user but continue */
else if((cgi_data->service_notification_types !=
AU_NOTIFICATION_SERVICE_ALL) &&
(!(cgi_data->object_types & AU_OBJTYPE_SERVICE))) {
result = RESULT_OPTION_IGNORED;
json_object_append_object(json_root, "result",
json_result(query_time, THISCGI,
svm_get_string_from_value(cgi_data->query, valid_queries),
get_query_status(query_status, cgi_data->query),
(time_t)-1, authinfo, result,
"The requested service notification types were ignored because service objects were not selected."));
}
return result;
}
int json_archive_alert_passes_selection(time_t timestamp, time_t start_time,
time_t end_time, int current_object_type, int match_object_types,
au_host *current_host, char *match_host, au_service *current_service,
char *match_service, int use_parent_host, host *parent_host,
int use_child_host, host *child_host, hostgroup *match_hostgroup,
servicegroup *match_servicegroup, contact *match_contact,
contactgroup *match_contactgroup, unsigned current_state_type,
unsigned match_state_types, unsigned current_state,
unsigned match_host_states, unsigned match_service_states) {
host *temp_host;
host *temp_host2;
service *temp_service;
contactgroupsmember *temp_contact_groupsmember;
int found;
if((start_time > 0) && (timestamp < start_time)) {
return 0;
}
if((end_time > 0) && (timestamp > end_time)) {
return 0;
}
/* Skip if we're not interested in the current state type */
if(!(current_state_type & match_state_types)) {
return 0;
}
switch(current_object_type) {
case AU_OBJTYPE_HOST:
/* Skip if we're not interested in hosts */
if(!(match_object_types & AU_OBJTYPE_HOST)) {
return 0;
}
/* Skip if user is not authorized for this host */
if((NULL != current_host->hostp) && (FALSE ==
is_authorized_for_host(current_host->hostp,
¤t_authdata))) {
return 0;
}
/* Skip if we're not interested in the current host state */
if(!(current_state & match_host_states)) {
return 0;
}
/* If a specific host name was specified, skip this host if it's
name does not match the one specified */
if((NULL != match_host) && strcmp(current_host->name, match_host)) {
return 0;
}
/* If a host parent was specified, skip this host if it's parent is
not the parent host specified */
if((1 == use_parent_host) && (NULL != current_host->hostp) &&
FALSE == is_host_immediate_child_of_host(parent_host,
current_host->hostp)) {
return 0;
}
/* If a hostgroup was specified, skip this host if it is not a member
of the hostgroup specified */
if((NULL != match_hostgroup) && (NULL != current_host->hostp) &&
( FALSE == is_host_member_of_hostgroup(match_hostgroup,
current_host->hostp))) {
return 0;
}
/* If the contact was specified, skip this host if it does not have
the contact specified */
if((NULL != match_contact) && (NULL != current_host->hostp) &&
(FALSE == is_contact_for_host(current_host->hostp,
match_contact))) {
return 0;
}
/* If the contact group was specified, skip this host if it does not
have the contact group specified */
if((NULL != match_contactgroup) && (NULL != current_host->hostp)) {
found = 0;
for(temp_contact_groupsmember =
current_host->hostp->contact_groups;
temp_contact_groupsmember != NULL;
temp_contact_groupsmember =
temp_contact_groupsmember->next) {
if(!strcmp(temp_contact_groupsmember->group_name,
match_contactgroup->group_name)) {
found = 1;
break;
}
}
if(0 == found) return 0;
}
/* If a child host was specified... */
if((1 == use_child_host) && (NULL != current_host->hostp)) {
/* If the child host is "none", skip this host if it has children */
if(NULL == child_host) {
for(temp_host2 = host_list; temp_host2 != NULL;
temp_host2 = temp_host2->next) {
if(TRUE ==
is_host_immediate_child_of_host(current_host->hostp,
temp_host2)) {
return 0;
}
}
}
/* Otherwise, skip this host if it does not have the specified host
as a child */
else if(FALSE ==
is_host_immediate_child_of_host(current_host->hostp,
child_host)) {
return 0;
}
}
break;
case AU_OBJTYPE_SERVICE:
/* Skip if we're not interested in services */
if(!(match_object_types & AU_OBJTYPE_SERVICE)) {
return 0;
}
/* Skip if user is not authorized for this service */
if((NULL != current_service->servicep) && (FALSE ==
is_authorized_for_service(current_service->servicep,
¤t_authdata))) {
return 0;
}
/* Skip if we're not interested in the current service state */
if(!(current_state & match_service_states)) {
return 0;
}
/* If a specific host name was specified, skip this service if it's
host name does not match the one specified */
if((NULL != match_host) && strcmp(current_service->host_name,
match_host)) {
return 0;
}
/* If a specific service description was specified, skip this service
if it's description does not match the one specified */
if((NULL != match_service) && strcmp(current_service->description,
match_service)) {
return 0;
}
/* If a host parent was specified, skip this service if the parent
of the host associated with it is not the parent host specified */
if((1 == use_parent_host) && (NULL != current_service->servicep)) {
temp_host = find_host(current_service->servicep->host_name);
if((NULL != temp_host) && (FALSE ==
is_host_immediate_child_of_host(parent_host, temp_host))) {
return 0;
}
}
/* If a hostgroup was specified, skip this service if it's host is not
a member of the hostgroup specified */
if((NULL != match_hostgroup) && (NULL != current_service->servicep)) {
temp_host = find_host(current_service->servicep->host_name);
if((NULL != temp_host) && (FALSE ==
is_host_member_of_hostgroup(match_hostgroup, temp_host))) {
return 0;
}
}
/* If a servicegroup was specified, skip this service if it is not
a member of the servicegroup specified */
if((NULL != match_servicegroup) &&
(NULL != current_service->servicep)) {
temp_service = find_service(current_service->servicep->host_name,
current_service->description);
if((NULL != temp_service) && (FALSE ==
is_service_member_of_servicegroup(match_servicegroup,
temp_service))) {
return 0;
}
}
/* If the contact was specified, skip this service if it does not have
the contact specified */
if((NULL != match_contact) && (NULL != current_service->servicep) &&
(FALSE == is_contact_for_service(current_service->servicep,
match_contact))) {
return 0;
}
/* If the contact group was specified, skip this service if it does not
have the contact group specified */
if((NULL != match_contactgroup) &&
(NULL != current_service->servicep)) {
found = 0;
for(temp_contact_groupsmember =
current_service->servicep->contact_groups;
temp_contact_groupsmember != NULL;
temp_contact_groupsmember =
temp_contact_groupsmember->next) {
if(!strcmp(temp_contact_groupsmember->group_name,
match_contactgroup->group_name)) {
found = 1;
break;
}
}
if(0 == found) return 0;
}
/* If a child host was specified... */
if((1 == use_child_host) && (NULL != current_service->servicep)) {
temp_host = find_host(current_service->servicep->host_name);
if(NULL != temp_host) {
/* If the child host is "none", skip this service if it's
host has children */
if(NULL == child_host) {
for(temp_host2 = host_list; temp_host2 != NULL;
temp_host2 = temp_host2->next) {
if(TRUE == is_host_immediate_child_of_host(temp_host,
temp_host2)) {
return 0;
}
}
}
/* Otherwise, skip this service if it's host does not have the
specified host as a child */
else if(FALSE == is_host_immediate_child_of_host(temp_host,
child_host)) {
return 0;
}
}
}
break;
}
return 1;
}
json_object * json_archive_alert_selectors(unsigned format_options, int start,
int count, time_t start_time, time_t end_time, int object_types,
char *match_host, char *match_service, int use_parent_host,
host *parent_host, int use_child_host, host *child_host,
hostgroup *match_hostgroup, servicegroup *match_servicegroup,
contact *match_contact, contactgroup *match_contactgroup,
unsigned match_state_types, unsigned match_host_states,
unsigned match_service_states) {
json_object *json_selectors;
json_selectors = json_new_object();
if( start > 0) {
json_object_append_integer(json_selectors, "start", start);
}
if( count > 0) {
json_object_append_integer(json_selectors, "count", count);
}
if(start_time > 0) {
json_object_append_time_t(json_selectors, "starttime", start_time);
}
if(end_time > 0) {
json_object_append_time_t(json_selectors, "endtime", end_time);
}
if(object_types != AU_OBJTYPE_ALL) {
json_bitmask(json_selectors, format_options, "objecttypes",
object_types, valid_object_types);
}
if(match_state_types != AU_STATETYPE_ALL) {
json_bitmask(json_selectors, format_options, "statetypes",
match_state_types, valid_state_types);
}
if((object_types & AU_OBJTYPE_HOST) &&
(match_host_states != AU_STATE_HOST_ALL)) {
json_bitmask(json_selectors, format_options, "hoststates",
match_host_states, valid_host_states);
}
if((object_types & AU_OBJTYPE_SERVICE) &&
(match_service_states != AU_STATE_SERVICE_ALL)) {
json_bitmask(json_selectors, format_options, "servicestates",
match_service_states, valid_service_states);
}
if(NULL != match_host) {
json_object_append_string(json_selectors, "hostname", &percent_escapes,
match_host);
}
if(NULL != match_service) {
json_object_append_string(json_selectors, "servicedescription",
&percent_escapes, match_service);
}
if(1 == use_parent_host) {
json_object_append_string(json_selectors, "parenthost",
&percent_escapes,
( NULL == parent_host ? "none" : parent_host->name));
}
if( 1 == use_child_host) {
json_object_append_string(json_selectors, "childhost", &percent_escapes,
( NULL == child_host ? "none" : child_host->name));
}
if(NULL != match_hostgroup) {
json_object_append_string(json_selectors, "hostgroup", &percent_escapes,
match_hostgroup->group_name);
}
if((object_types & AU_OBJTYPE_SERVICE) && (NULL != match_servicegroup)) {
json_object_append_string(json_selectors, "servicegroup",
&percent_escapes, match_servicegroup->group_name);
}
if(NULL != match_contact) {
json_object_append_string(json_selectors, "contact", &percent_escapes,
match_contact->name);
}
if(NULL != match_contactgroup) {
json_object_append_string(json_selectors, "contactgroup",
&percent_escapes, match_contactgroup->group_name);
}
return json_selectors;
}
json_object * json_archive_alertcount(unsigned format_options,
time_t start_time, time_t end_time, int object_types,
char *match_host, char *match_service, int use_parent_host,
host *parent_host, int use_child_host, host *child_host,
hostgroup *match_hostgroup, servicegroup *match_servicegroup,
contact *match_contact, contactgroup *match_contactgroup,
unsigned match_state_types, unsigned match_host_states,
unsigned match_service_states, au_log *log) {
json_object *json_data;
au_node *temp_node;
au_log_entry *temp_entry;
au_log_alert *temp_alert_log;
au_host *temp_host = NULL;
au_service *temp_service = NULL;
int count = 0;
json_data = json_new_object();
json_object_append_object(json_data, "selectors",
json_archive_alert_selectors(format_options, 0, 0, start_time,
end_time, object_types, match_host, match_service, use_parent_host,
parent_host, use_child_host, child_host, match_hostgroup,
match_servicegroup, match_contact, match_contactgroup,
match_state_types, match_host_states, match_service_states));
for(temp_node = log->entry_list->head; temp_node != NULL;
temp_node = temp_node->next) {
temp_entry = (au_log_entry *)temp_node->data;
/* Skip all but alert type messages */
if(AU_LOGTYPE_ALERT != temp_entry->entry_type) continue;
/* Get the host/service object */
temp_alert_log = (au_log_alert *)temp_entry->entry;
switch(temp_alert_log->obj_type) {
case AU_OBJTYPE_HOST:
temp_host = (au_host *)temp_alert_log->object;
temp_service = NULL;
break;
case AU_OBJTYPE_SERVICE:
temp_host = NULL;
temp_service = (au_service *)temp_alert_log->object;
break;
}
/* Skip anything that does not pass the alert selection criteria */
if(json_archive_alert_passes_selection(temp_entry->timestamp,
start_time, end_time, temp_alert_log->obj_type,
object_types, temp_host, match_host, temp_service,
match_service, use_parent_host, parent_host, use_child_host,
child_host, match_hostgroup, match_servicegroup,
match_contact, match_contactgroup, temp_alert_log->state_type,
match_state_types, temp_alert_log->state, match_host_states,
match_service_states) == 0) {
continue;
}
count++;
}
json_object_append_integer(json_data, "count", count);
return json_data;
}
json_object * json_archive_alertlist(unsigned format_options, int start,
int count, time_t start_time, time_t end_time, int object_types,
char *match_host, char *match_service, int use_parent_host,
host *parent_host, int use_child_host, host *child_host,
hostgroup *match_hostgroup, servicegroup *match_servicegroup,
contact *match_contact, contactgroup *match_contactgroup,
unsigned match_state_types, unsigned match_host_states,
unsigned match_service_states, au_log *log) {
json_object *json_data;
json_array *json_alertlist;
json_object *json_alert_details;
au_node *temp_node;
au_log_entry *temp_entry;
au_log_alert *temp_alert_log;
au_host *temp_host = NULL;
au_service *temp_service = NULL;
int current = 0;
int counted = 0;
json_data = json_new_object();
json_object_append_object(json_data, "selectors",
json_archive_alert_selectors(format_options, start, count,
start_time, end_time, object_types, match_host, match_service,
use_parent_host, parent_host, use_child_host, child_host,
match_hostgroup, match_servicegroup, match_contact,
match_contactgroup, match_state_types, match_host_states,
match_service_states));
json_alertlist = json_new_array();
for(temp_node = log->entry_list->head; temp_node != NULL;
temp_node = temp_node->next) {
temp_entry = (au_log_entry *)temp_node->data;
/* Skip all but alert type messages */
if(AU_LOGTYPE_ALERT != temp_entry->entry_type) continue;
/* Get the host/service object */
temp_alert_log = (au_log_alert *)temp_entry->entry;
switch(temp_alert_log->obj_type) {
case AU_OBJTYPE_HOST:
temp_host = (au_host *)temp_alert_log->object;
temp_service = NULL;
break;
case AU_OBJTYPE_SERVICE:
temp_host = NULL;
temp_service = (au_service *)temp_alert_log->object;
break;
}
/* Skip anything that does not pass the alert selection criteria */
if(json_archive_alert_passes_selection(temp_entry->timestamp,
start_time, end_time, temp_alert_log->obj_type,
object_types, temp_host, match_host, temp_service,
match_service, use_parent_host, parent_host, use_child_host,
child_host, match_hostgroup, match_servicegroup,
match_contact, match_contactgroup, temp_alert_log->state_type,
match_state_types, temp_alert_log->state, match_host_states,
match_service_states) == 0) {
continue;
}
/* If the current item passes the start and limit tests, display it */
if( passes_start_and_count_limits(start, count, current, counted)) {
json_alert_details = json_new_object();
json_archive_alert_details(json_alert_details, format_options,
temp_entry->timestamp, (au_log_alert *)temp_entry->entry);
json_array_append_object(json_alertlist, json_alert_details);
counted++;
}
current++;
}
json_object_append_array(json_data, "alertlist", json_alertlist);
return json_data;
}
void json_archive_alert_details(json_object *json_details,
unsigned format_options, time_t timestamp, au_log_alert *temp_alert) {
au_host *temp_host;
au_service *temp_service;
json_object_append_time_t(json_details, "timestamp", timestamp);
json_enumeration(json_details, format_options, "object_type",
temp_alert->obj_type, svm_au_object_types);
switch(temp_alert->obj_type) {
case AU_OBJTYPE_HOST:
temp_host = (au_host *)temp_alert->object;
json_object_append_string(json_details, "name", &percent_escapes,
temp_host->name);
break;
case AU_OBJTYPE_SERVICE:
temp_service = (au_service *)temp_alert->object;
json_object_append_string(json_details, "host_name", &percent_escapes,
temp_service->host_name);
json_object_append_string(json_details, "description", &percent_escapes,
temp_service->description);
break;
}
json_enumeration(json_details, format_options, "state_type",
temp_alert->state_type, svm_au_state_types);
json_enumeration(json_details, format_options, "state", temp_alert->state,
svm_au_states);
json_object_append_string(json_details, "plugin_output", &percent_escapes,
temp_alert->plugin_output);
}
int json_archive_notification_passes_selection(time_t timestamp,
time_t start_time, time_t end_time, int current_object_type,
int match_object_types, au_host *current_host, char *match_host,
au_service *current_service, char *match_service, int use_parent_host,
host *parent_host, int use_child_host, host *child_host,
hostgroup *match_hostgroup, servicegroup *match_servicegroup,
au_contact *current_contact, char *match_contact,
contactgroup *match_contactgroup,
unsigned current_notification_type,
unsigned match_host_notification_types,
unsigned match_service_notification_types,
char *current_notification_method, char *match_notification_method) {
host *temp_host;
host *temp_host2;
service *temp_service;
contactgroupsmember *temp_contact_groupsmember;
int found;
if((start_time > 0) && (timestamp < start_time)) {
return 0;
}
if((end_time > 0) && (timestamp > end_time)) {
return 0;
}
/* If the contact was specified, skip this notification if it does not have
the contact specified */
if((NULL != match_contact) && (NULL != current_contact) &&
strcmp(current_contact->name, match_contact)) {
return 0;
}
/* If the notification method was specified, skip this notification if
it does not have the method specified */
if((NULL != match_notification_method) &&
(NULL != current_notification_method) &&
strcmp(current_notification_method, match_notification_method)) {
return 0;
}
switch(current_object_type) {
case AU_OBJTYPE_HOST:
/* Skip if we're not interested in hosts */
if(!(match_object_types & AU_OBJTYPE_HOST)) {
return 0;
}
/* Skip if user is not authorized for this host */
if((NULL != current_host->hostp) && (FALSE ==
is_authorized_for_host(current_host->hostp,
¤t_authdata))) {
return 0;
}
/* Skip if we're not interested in the current host notification type */
if(!(current_notification_type & match_host_notification_types)) {
return 0;
}
/* If a specific host name was specified, skip this host if it's
name does not match the one specified */
if((NULL != match_host) && strcmp(current_host->name, match_host)) {
return 0;
}
/* If a host parent was specified, skip this host if it's parent is
not the parent host specified */
if((1 == use_parent_host) && (NULL != current_host->hostp) &&
FALSE == is_host_immediate_child_of_host(parent_host,
current_host->hostp)) {
return 0;
}
/* If a hostgroup was specified, skip this host if it is not a member
of the hostgroup specified */
if((NULL != match_hostgroup) && (NULL != current_host->hostp) &&
( FALSE == is_host_member_of_hostgroup(match_hostgroup,
current_host->hostp))) {
return 0;
}
/* If the contact group was specified, skip this host if it does not
have the contact group specified */
if((NULL != match_contactgroup) && (NULL != current_host->hostp)) {
found = 0;
for(temp_contact_groupsmember =
current_host->hostp->contact_groups;
temp_contact_groupsmember != NULL;
temp_contact_groupsmember =
temp_contact_groupsmember->next) {
if(!strcmp(temp_contact_groupsmember->group_name,
match_contactgroup->group_name)) {
found = 1;
break;
}
}
if(0 == found) return 0;
}
/* If a child host was specified... */
if((1 == use_child_host) && (NULL != current_host->hostp)) {
/* If the child host is "none", skip this host if it has children */
if(NULL == child_host) {
for(temp_host2 = host_list; temp_host2 != NULL;
temp_host2 = temp_host2->next) {
if(TRUE ==
is_host_immediate_child_of_host(current_host->hostp,
temp_host2)) {
return 0;
}
}
}
/* Otherwise, skip this host if it does not have the specified host
as a child */
else if(FALSE ==
is_host_immediate_child_of_host(current_host->hostp,
child_host)) {
return 0;
}
}
break;
case AU_OBJTYPE_SERVICE:
/* Skip if we're not interested in services */
if(!(match_object_types & AU_OBJTYPE_SERVICE)) {
return 0;
}
/* Skip if user is not authorized for this service */
if((NULL != current_service->servicep) && (FALSE ==
is_authorized_for_service(current_service->servicep,
¤t_authdata))) {
return 0;
}
/* Skip if we're not interested in the current service notification
type */
if(!(current_notification_type & match_service_notification_types)) {
return 0;
}
/* If a specific host name was specified, skip this service if it's
host name does not match the one specified */
if((NULL != match_host) && strcmp(current_service->host_name,
match_host)) {
return 0;
}
/* If a specific service description was specified, skip this service
if it's description does not match the one specified */
if((NULL != match_service) && strcmp(current_service->description,
match_service)) {
return 0;
}
/* If a host parent was specified, skip this service if the parent
of the host associated with it is not the parent host specified */
if((1 == use_parent_host) && (NULL != current_service->servicep)) {
temp_host = find_host(current_service->servicep->host_name);
if((NULL != temp_host) && (FALSE ==
is_host_immediate_child_of_host(parent_host, temp_host))) {
return 0;
}
}
/* If a hostgroup was specified, skip this service if it's host is not
a member of the hostgroup specified */
if((NULL != match_hostgroup) && (NULL != current_service->servicep)) {
temp_host = find_host(current_service->servicep->host_name);
if((NULL != temp_host) && (FALSE ==
is_host_member_of_hostgroup(match_hostgroup, temp_host))) {
return 0;
}
}
/* If a servicegroup was specified, skip this service if it is not
a member of the servicegroup specified */
if((NULL != match_servicegroup) &&
(NULL != current_service->servicep)) {
temp_service = find_service(current_service->servicep->host_name,
current_service->description);
if((NULL != temp_service) && (FALSE ==
is_service_member_of_servicegroup(match_servicegroup,
temp_service))) {
return 0;
}
}
/* If the contact group was specified, skip this service if it does not
have the contact group specified */
if((NULL != match_contactgroup) &&
(NULL != current_service->servicep)) {
found = 0;
for(temp_contact_groupsmember =
current_service->servicep->contact_groups;
temp_contact_groupsmember != NULL;
temp_contact_groupsmember =
temp_contact_groupsmember->next) {
if(!strcmp(temp_contact_groupsmember->group_name,
match_contactgroup->group_name)) {
found = 1;
break;
}
}
if(0 == found) return 0;
}
/* If a child host was specified... */
if((1 == use_child_host) && (NULL != current_service->servicep)) {
temp_host = find_host(current_service->servicep->host_name);
if(NULL != temp_host) {
/* If the child host is "none", skip this service if it's
host has children */
if(NULL == child_host) {
for(temp_host2 = host_list; temp_host2 != NULL;
temp_host2 = temp_host2->next) {
if(TRUE == is_host_immediate_child_of_host(temp_host,
temp_host2)) {
return 0;
}
}
}
/* Otherwise, skip this service if it's host does not have the
specified host as a child */
else if(FALSE == is_host_immediate_child_of_host(temp_host,
child_host)) {
return 0;
}
}
}
break;
}
return 1;
}
json_object * json_archive_notification_selectors(unsigned format_options,
int start, int count, time_t start_time, time_t end_time,
int match_object_types, char *match_host, char *match_service,
int use_parent_host, host *parent_host, int use_child_host,
host *child_host, hostgroup *match_hostgroup,
servicegroup *match_servicegroup, char *match_contact,
contactgroup *match_contactgroup,
unsigned match_host_notification_types,
unsigned match_service_notification_types,
char *match_notification_method) {
json_object *json_selectors;
json_selectors = json_new_object();
if( start > 0) {
json_object_append_integer(json_selectors, "start", start);
}
if( count > 0) {
json_object_append_integer(json_selectors, "count", count);
}
if(start_time > 0) {
json_object_append_time_t(json_selectors, "starttime", start_time);
}
if(end_time > 0) {
json_object_append_time_t(json_selectors, "endtime", end_time);
}
if(match_object_types != AU_OBJTYPE_ALL) {
json_bitmask(json_selectors, format_options, "objecttypes",
match_object_types, valid_object_types);
}
if((match_object_types & AU_OBJTYPE_HOST) &&
(match_host_notification_types != AU_NOTIFICATION_HOST_ALL)) {
json_bitmask(json_selectors, format_options, "hostnotificationtypes",
match_host_notification_types,
valid_host_notification_types);
}
if((match_object_types & AU_OBJTYPE_SERVICE) &&
(match_service_notification_types != AU_NOTIFICATION_SERVICE_ALL)) {
json_bitmask(json_selectors, format_options, "servicenotificationtypes",
match_service_notification_types,
valid_service_notification_types);
}
if(NULL != match_host) {
json_object_append_string(json_selectors, "hostname", &percent_escapes,
match_host);
}
if(NULL != match_service) {
json_object_append_string(json_selectors, "servicedescription",
&percent_escapes, match_service);
}
if(1 == use_parent_host) {
json_object_append_string(json_selectors, "parenthost",
&percent_escapes,
( NULL == parent_host ? "none" : parent_host->name));
}
if( 1 == use_child_host) {
json_object_append_string(json_selectors, "childhost",
&percent_escapes,
( NULL == child_host ? "none" : child_host->name));
}
if(NULL != match_hostgroup) {
json_object_append_string(json_selectors, "hostgroup", &percent_escapes,
match_hostgroup->group_name);
}
if((match_object_types & AU_OBJTYPE_SERVICE) &&
(NULL != match_servicegroup)) {
json_object_append_string(json_selectors, "servicegroup",
&percent_escapes, match_servicegroup->group_name);
}
if(NULL != match_contact) {
json_object_append_string(json_selectors, "contact", &percent_escapes,
match_contact);
}
if(NULL != match_contactgroup) {
json_object_append_string(json_selectors, "contactgroup",
&percent_escapes, match_contactgroup->group_name);
}
if(NULL != match_notification_method) {
json_object_append_string(json_selectors, "notificationmethod",
&percent_escapes, match_notification_method);
}
return json_selectors;
}
json_object * json_archive_notificationcount(unsigned format_options,
time_t start_time, time_t end_time, int match_object_types,
char *match_host, char *match_service, int use_parent_host,
host *parent_host, int use_child_host, host *child_host,
hostgroup *match_hostgroup, servicegroup *match_servicegroup,
char *match_contact, contactgroup *match_contactgroup,
unsigned match_host_notification_types,
unsigned match_service_notification_types,
char *match_notification_method, au_log *log) {
json_object *json_data;
au_node *temp_node;
au_log_entry *temp_entry;
au_log_notification *temp_notification_log;
au_host *temp_host = NULL;
au_service *temp_service = NULL;
int count = 0;
json_data = json_new_object();
json_object_append_object(json_data, "selectors",
json_archive_notification_selectors(format_options, 0, 0,
start_time, end_time, match_object_types, match_host,
match_service, use_parent_host, parent_host, use_child_host,
child_host, match_hostgroup, match_servicegroup, match_contact,
match_contactgroup, match_host_notification_types,
match_service_notification_types, match_notification_method));
for(temp_node = log->entry_list->head; temp_node != NULL;
temp_node = temp_node->next) {
temp_entry = (au_log_entry *)temp_node->data;
/* Skip all but notification type messages */
if(AU_LOGTYPE_NOTIFICATION != temp_entry->entry_type) continue;
/* Get the host/service object */
temp_notification_log = (au_log_notification *)temp_entry->entry;
switch(temp_notification_log->obj_type) {
case AU_OBJTYPE_HOST:
temp_host = (au_host *)temp_notification_log->object;
temp_service = NULL;
break;
case AU_OBJTYPE_SERVICE:
temp_host = NULL;
temp_service = (au_service *)temp_notification_log->object;
break;
}
/* Skip anything that does not pass the notification selection
criteria */
if(json_archive_notification_passes_selection(temp_entry->timestamp,
start_time, end_time, temp_notification_log->obj_type,
match_object_types, temp_host, match_host, temp_service,
match_service, use_parent_host, parent_host, use_child_host,
child_host, match_hostgroup, match_servicegroup,
temp_notification_log->contact, match_contact,
match_contactgroup, temp_notification_log->notification_type,
match_host_notification_types,
match_service_notification_types, temp_notification_log->method, match_notification_method) == 0) {
continue;
}
count++;
}
json_object_append_integer(json_data, "count", count);
return json_data;
}
json_object * json_archive_notificationlist(unsigned format_options, int start,
int count, time_t start_time, time_t end_time, int match_object_types,
char *match_host, char *match_service, int use_parent_host,
host *parent_host, int use_child_host, host *child_host,
hostgroup *match_hostgroup, servicegroup *match_servicegroup,
char *match_contact, contactgroup *match_contactgroup,
unsigned match_host_notification_types,
unsigned match_service_notification_types,
char *match_notification_method, au_log *log) {
json_object *json_data;
json_array *json_notificationlist;
json_object *json_notification_details;
au_node *temp_node;
au_log_entry *temp_entry;
au_log_notification *temp_notification_log;
au_host *temp_host = NULL;
au_service *temp_service = NULL;
int current = 0;
int counted = 0;
json_data = json_new_object();
json_object_append_object(json_data, "selectors",
json_archive_notification_selectors(format_options, start, count,
start_time, end_time, match_object_types, match_host,
match_service, use_parent_host, parent_host, use_child_host,
child_host, match_hostgroup, match_servicegroup, match_contact,
match_contactgroup, match_host_notification_types,
match_service_notification_types, match_notification_method));
json_notificationlist = json_new_array();
for(temp_node = log->entry_list->head; temp_node != NULL;
temp_node = temp_node->next) {
temp_entry = (au_log_entry *)temp_node->data;
/* Skip all but notification type messages */
if(AU_LOGTYPE_NOTIFICATION != temp_entry->entry_type) continue;
/* Get the host/service object */
temp_notification_log = (au_log_notification *)temp_entry->entry;
switch(temp_notification_log->obj_type) {
case AU_OBJTYPE_HOST:
temp_host = (au_host *)temp_notification_log->object;
temp_service = NULL;
break;
case AU_OBJTYPE_SERVICE:
temp_host = NULL;
temp_service = (au_service *)temp_notification_log->object;
break;
}
/* Skip anything that does not pass the notification selection
criteria */
if(json_archive_notification_passes_selection(temp_entry->timestamp,
start_time, end_time, temp_notification_log->obj_type,
match_object_types, temp_host, match_host, temp_service,
match_service, use_parent_host, parent_host, use_child_host,
child_host, match_hostgroup, match_servicegroup,
temp_notification_log->contact, match_contact,
match_contactgroup, temp_notification_log->notification_type,
match_host_notification_types,
match_service_notification_types, temp_notification_log->method,
match_notification_method) == 0) {
continue;
}
/* If the current item passes the start and limit tests, display it */
if( passes_start_and_count_limits(start, count, current, counted)) {
json_notification_details = json_new_object();
json_archive_notification_details(json_notification_details,
format_options, temp_entry->timestamp,
(au_log_notification *)temp_entry->entry);
json_array_append_object(json_notificationlist,
json_notification_details);
counted++;
}
current++;
}
json_object_append_array(json_data, "notificationlist",
json_notificationlist);
return json_data;
}
void json_archive_notification_details(json_object *json_details,
unsigned format_options, time_t timestamp,
au_log_notification *temp_notification) {
au_host *temp_host;
au_service *temp_service;
json_object_append_time_t(json_details, "timestamp", timestamp);
json_enumeration(json_details, format_options, "object_type",
temp_notification->obj_type,
svm_au_object_types);
switch(temp_notification->obj_type) {
case AU_OBJTYPE_HOST:
temp_host = (au_host *)temp_notification->object;
json_object_append_string(json_details, "name", &percent_escapes,
temp_host->name);
break;
case AU_OBJTYPE_SERVICE:
temp_service = (au_service *)temp_notification->object;
json_object_append_string(json_details, "host_name", &percent_escapes,
temp_service->host_name);
json_object_append_string(json_details, "description", &percent_escapes,
temp_service->description);
break;
}
json_object_append_string(json_details, "contact", &percent_escapes,
temp_notification->contact->name);
json_enumeration(json_details, format_options, "notification_type",
temp_notification->notification_type, svm_au_notification_types);
json_object_append_string(json_details, "method", &percent_escapes,
temp_notification->method);
json_object_append_string(json_details, "message", &percent_escapes,
temp_notification->message);
}
int json_archive_statechange_passes_selection(time_t timestamp,
time_t end_time, int current_object_type,
int match_object_type, au_host *current_host, char *match_host,
au_service *current_service, char *match_service,
unsigned current_state_type, unsigned match_state_types) {
if((end_time > 0) && (timestamp > end_time)) {
return 0;
}
/* Skip if we're not interested in the current state type */
if(!(current_state_type & match_state_types)) {
return 0;
}
switch(current_object_type) {
case AU_OBJTYPE_HOST:
/* Skip if we're not interested in hosts */
if(match_object_type != AU_OBJTYPE_HOST) {
return 0;
}
/* Skip if user is not authorized for this host */
if((NULL != current_host->hostp) && (FALSE ==
is_authorized_for_host(current_host->hostp,
¤t_authdata))) {
return 0;
}
/* If a specific host name was specified, skip this host if it's
name does not match the one specified */
if((NULL != match_host) && (NULL != current_host) &&
strcmp(current_host->name, match_host)) {
return 0;
}
break;
case AU_OBJTYPE_SERVICE:
/* Skip if we're not interested in services */
if(match_object_type != AU_OBJTYPE_SERVICE) {
return 0;
}
/* Skip if user is not authorized for this service */
if((NULL != current_service->servicep) && (FALSE ==
is_authorized_for_service(current_service->servicep,
¤t_authdata))) {
return 0;
}
/* If a specific service was specified, skip this service if it's
host name and service description do not match the one specified */
if((NULL != match_host) && (NULL != match_service) &&
(NULL != current_service) &&
( strcmp(current_service->host_name, match_host) ||
strcmp(current_service->description, match_service))) {
return 0;
}
break;
}
return 1;
}
json_object *json_archive_statechange_selectors(unsigned format_options,
int start, int count, time_t start_time, time_t end_time,
int object_type, char *host_name, char *service_description,
unsigned state_types) {
json_object *json_selectors;
json_selectors = json_new_object();
if( start > 0) {
json_object_append_integer(json_selectors, "start", start);
}
if( count > 0) {
json_object_append_integer(json_selectors, "count", count);
}
if(start_time > 0) {
json_object_append_time_t(json_selectors, "starttime", start_time);
}
if(end_time > 0) {
json_object_append_time_t(json_selectors, "endtime", end_time);
}
if(NULL != host_name) {
json_object_append_string(json_selectors, "hostname", &percent_escapes,
host_name);
}
if(NULL != service_description) {
json_object_append_string(json_selectors, "servicedescription",
&percent_escapes, service_description);
}
if(state_types != AU_STATETYPE_ALL) {
json_bitmask(json_selectors, format_options, "statetypes", state_types,
valid_state_types);
}
return json_selectors;
}
json_object *json_archive_statechangelist(unsigned format_options,
int start, int count, time_t start_time, time_t end_time,
int object_type, char *host_name, char *service_description,
int assumed_initial_host_state, int assumed_initial_service_state,
unsigned state_types, au_log *log) {
json_object *json_data;
json_array *json_statechangelist;
json_object *json_statechange_details;
au_node *temp_node;
int initial_host_state = AU_STATE_NO_DATA;
int initial_service_state = AU_STATE_NO_DATA;
au_log_entry *temp_entry = NULL;
au_log_alert *temp_state_log = NULL;
au_log_alert *start_log = NULL;
au_log_alert *end_log = NULL;
au_host *temp_host = NULL;
au_service *temp_service = NULL;
int have_seen_first_entry = 0;
int current = 0;
int counted = 0;
if(assumed_initial_host_state != AU_STATE_NO_DATA)
initial_host_state = assumed_initial_host_state;
if(assumed_initial_service_state != AU_STATE_NO_DATA)
initial_service_state = assumed_initial_service_state;
json_data = json_new_object();
json_object_append_object(json_data, "selectors",
json_archive_statechange_selectors(format_options, start, count,
start_time, end_time, object_type, host_name, service_description,
state_types));
json_statechangelist = json_new_array();
for(temp_node = log->entry_list->head; temp_node != NULL;
temp_node = temp_node->next) {
/* Skip all but notification type messages */
temp_entry = (au_log_entry *)temp_node->data;
if(!(temp_entry->entry_type & (AU_LOGTYPE_STATE | AU_LOGTYPE_ALERT))) {
continue;
}
/* Get the host/service object */
temp_state_log = (au_log_alert *)temp_entry->entry;
switch(temp_state_log->obj_type) {
case AU_OBJTYPE_HOST:
temp_host = (au_host *)temp_state_log->object;
temp_service = NULL;
break;
case AU_OBJTYPE_SERVICE:
temp_host = NULL;
temp_service = (au_service *)temp_state_log->object;
break;
}
/* Skip any entries not passing the selectors */
if(json_archive_statechange_passes_selection(temp_entry->timestamp,
end_time, temp_state_log->obj_type,
object_type, temp_host, host_name, temp_service,
service_description, temp_state_log->state_type,
state_types) == 0) {
continue;
}
if((start_time > 0) && (temp_entry->timestamp < start_time)) {
/* If we're before the start time and no initial assumed state
was provided, record the state to be used as the initial
state */
switch(temp_state_log->obj_type) {
case AU_OBJTYPE_HOST:
if(AU_STATE_NO_DATA == assumed_initial_host_state)
initial_host_state = temp_state_log->state;
break;
case AU_OBJTYPE_SERVICE:
if(AU_STATE_NO_DATA == assumed_initial_service_state)
initial_service_state = temp_state_log->state;
break;
}
continue;
}
else if(0 == have_seen_first_entry) {
/* When we get to the first entry after the start, inject a
pseudo entry with the initial state */
switch(object_type) {
case AU_OBJTYPE_HOST:
start_log = au_create_alert_or_state_log(object_type,
temp_host, AU_STATETYPE_HARD, initial_host_state,
"Initial Host Pseudo-State");
break;
case AU_OBJTYPE_SERVICE:
start_log = au_create_alert_or_state_log(object_type,
temp_service, AU_STATETYPE_HARD, initial_service_state,
"Initial Service Pseudo-State");
break;
}
if(start_log != NULL) {
json_statechange_details = json_new_object();
json_archive_alert_details(json_statechange_details,
format_options, start_time, start_log);
json_array_append_object(json_statechangelist,
json_statechange_details);
au_free_alert_log(start_log);
}
have_seen_first_entry = 1;
}
#if 0
else {
/* Ignore ALERT logs if their state is not different from the
previous state */
switch(object_type) {
case AU_OBJTYPE_HOST:
// if((temp_entry->entry_type & (AU_LOGTYPE_ALERT |
// AU_LOGTYPE_STATE_INITIAL)) &&
if((temp_entry->entry_type & AU_LOGTYPE_ALERT) &&
(last_host_state == temp_state_log->state)) {
continue;
}
last_host_state = temp_state_log->state;
break;
case AU_OBJTYPE_SERVICE:
// if((temp_entry->entry_type & (AU_LOGTYPE_ALERT |
// AU_LOGTYPE_STATE_INITIAL)) &&
if((temp_entry->entry_type & AU_LOGTYPE_ALERT) &&
(last_service_state == temp_state_log->state)) {
continue;
}
last_service_state = temp_state_log->state;
break;
}
}
#endif
/* If the current item passes the start and limit tests, display it */
if( passes_start_and_count_limits(start, count, current, counted)) {
json_statechange_details = json_new_object();
json_archive_alert_details(json_statechange_details,
format_options, temp_entry->timestamp, temp_state_log);
json_array_append_object(json_statechangelist,
json_statechange_details);
counted++;
}
current++;
}
/* Inject a pseudo entry with the final state */
switch(object_type) {
case AU_OBJTYPE_HOST:
temp_host = au_find_host(log->hosts, host_name);
if(NULL != temp_host) {
end_log = au_create_alert_or_state_log(object_type,
temp_host, AU_STATETYPE_HARD, temp_state_log->state,
"Final Host Pseudo-State");
}
break;
case AU_OBJTYPE_SERVICE:
temp_service = au_find_service(log->hosts, host_name,
service_description);
if(NULL != temp_service) {
end_log = au_create_alert_or_state_log(object_type,
temp_service, AU_STATETYPE_HARD, temp_state_log->state,
"Final Service Pseudo-State");
}
break;
}
if(end_log != NULL) {
json_statechange_details = json_new_object();
json_archive_alert_details(json_statechange_details, format_options,
end_time, end_log);
json_array_append_object(json_statechangelist,
json_statechange_details);
au_free_alert_log(end_log);
}
json_object_append_array(json_data, "statechangelist",
json_statechangelist);
return json_data;
}
/*
Return the initial state of Nagios itself.
*/
int get_initial_nagios_state(au_linked_list *log_entries, time_t start_time,
time_t end_time) {
au_node *temp_node;
au_log_entry *current_log_entry;
au_log_nagios *temp_nagios_log;
int initial_state = AU_STATE_NO_DATA;
for(temp_node = log_entries->head; temp_node != NULL;
temp_node = temp_node->next) {
current_log_entry = (au_log_entry *)temp_node->data;
if(current_log_entry->timestamp < start_time) {
/* Any log entries prior to the start time tell us something
about the initial state of Nagios so look at all of them */
switch(current_log_entry->entry_type) {
case AU_LOGTYPE_NAGIOS:
/* If the log is a Nagios start or stop log, that is the
current program state */
temp_nagios_log = (au_log_nagios *)current_log_entry->entry;
initial_state = temp_nagios_log->type;
break;
default:
/* Any other log indicates that Nagios is running */
initial_state = AU_STATE_PROGRAM_START;
break;
}
}
else {
if(AU_STATE_NO_DATA != initial_state) {
/* Once we cross the threshold of the start time, if we have
an initial state, that is THE initial state */
return initial_state;
}
else {
/* Otherwise the first log encountered tells us the initial
state of Nagios */
switch(current_log_entry->entry_type) {
case AU_LOGTYPE_NAGIOS:
/* If the log is a Nagios start or stop log, that is
opposite the initial state */
temp_nagios_log = (au_log_nagios *)current_log_entry->entry;
switch(temp_nagios_log->type) {
case AU_STATE_PROGRAM_START:
return AU_STATE_PROGRAM_END;
break;
case AU_STATE_PROGRAM_END:
return AU_STATE_PROGRAM_START;
break;
}
break;
default:
/* Any other log indicates that Nagios was running at
the start time */
return AU_STATE_PROGRAM_START;
break;
}
}
}
}
/* If we reach this point, we are in bad shape because we had nothing
in the logs and the initial state should still be AU_STATE_NO_DATA,
which we return as an error indication */
return initial_state;
}
/*
Return the initial downtime state of the host or service.
*/
int get_initial_downtime_state(au_linked_list *log_entries, time_t start_time,
time_t end_time) {
au_node *temp_node;
au_log_entry *current_log_entry;
au_log_downtime *temp_downtime_log;
int initial_state = AU_STATE_NO_DATA;
for(temp_node = log_entries->head; temp_node != NULL;
temp_node = temp_node->next) {
current_log_entry = (au_log_entry *)temp_node->data;
if(current_log_entry->timestamp < start_time) {
/* Any downtime log prior to the start time may indicate the
initial downtime state so look at all of them */
if(AU_LOGTYPE_DOWNTIME == current_log_entry->entry_type) {
temp_downtime_log = (au_log_downtime *)current_log_entry->entry;
initial_state = temp_downtime_log->downtime_type;
}
}
else if(current_log_entry->timestamp <= end_time) {
if(AU_STATE_NO_DATA != initial_state) {
/* Once we cross the start time, if we have a downtime state,
we have THE initial downtime state */
return initial_state;
}
else {
/* If we don't have a downtime state yet, the first downtime
state we encounter will be opposite the initial downtime
state */
if(AU_LOGTYPE_NOTIFICATION == current_log_entry->entry_type) {
temp_downtime_log = (au_log_downtime *)current_log_entry->entry;
switch(temp_downtime_log->downtime_type) {
case AU_STATE_DOWNTIME_START:
return AU_STATE_DOWNTIME_END;
break;
case AU_STATE_DOWNTIME_END:
return AU_STATE_DOWNTIME_START;
break;
}
}
}
}
}
/* If we haven't encountered any indication of downtime yet, assume
we were not initially in downtime */
if(AU_STATE_NO_DATA == initial_state) {
initial_state = AU_STATE_DOWNTIME_END;
}
return initial_state;
}
/*
Return the initial state of the host or service.
*/
int get_initial_subject_state(au_linked_list *log_entries, time_t start_time,
time_t end_time) {
au_node *temp_node;
au_log_entry *current_log_entry;
int initial_state = AU_STATE_NO_DATA;
for(temp_node = log_entries->head; temp_node != NULL;
temp_node = temp_node->next) {
current_log_entry = (au_log_entry *)temp_node->data;
if(current_log_entry->timestamp < start_time) {
/* Any state log prior to the start time may indicate the
initial state of the subject so look at all of them */
switch(current_log_entry->entry_type) {
case AU_LOGTYPE_ALERT:
case AU_LOGTYPE_STATE_INITIAL:
case AU_LOGTYPE_STATE_CURRENT:
initial_state = get_log_entry_state(current_log_entry);
break;
}
}
}
return initial_state;
}
int get_log_entry_state(au_log_entry *log_entry) {
switch(log_entry->entry_type) {
case AU_LOGTYPE_ALERT:
case AU_LOGTYPE_STATE_INITIAL:
case AU_LOGTYPE_STATE_CURRENT:
return ((au_log_alert *)log_entry->entry)->state;
break;
case AU_LOGTYPE_DOWNTIME:
return ((au_log_downtime *)log_entry->entry)->downtime_type;
break;
case AU_LOGTYPE_NAGIOS:
return ((au_log_nagios *)log_entry->entry)->type;
break;
case AU_LOGTYPE_NOTIFICATION:
return ((au_log_notification *)log_entry->entry)->notification_type;
break;
default:
return AU_STATE_NO_DATA;
break;
}
}
#define have_real_data(a) have_data((a), \
~(AU_STATE_NO_DATA | AU_STATE_PROGRAM_START | AU_STATE_PROGRAM_END))
#define have_state_data(a) have_data((a), AU_STATE_ALL)
int have_data(au_linked_list *log_entries, int mask) {
au_node *temp_node;
int state;
for(temp_node = log_entries->head; temp_node != NULL;
temp_node = temp_node->next) {
state = get_log_entry_state((au_log_entry *)temp_node->data);
if(state & mask) return TRUE;
}
return FALSE;
}
unsigned long calculate_window_duration(time_t start_time, time_t end_time,
timeperiod *report_timeperiod) {
struct tm *t;
unsigned long state_duration;
time_t midnight;
int weekday;
timerange *temp_timerange;
unsigned long temp_duration;
unsigned long temp_end;
unsigned long temp_start;
unsigned long start;
unsigned long end;
/* MickeM - attempt to handle the the report time_period (if any) */
if(report_timeperiod != NULL) {
t = localtime((time_t *)&start_time);
state_duration = 0;
/* calculate the start of the day (midnight, 00:00 hours) */
t->tm_sec = 0;
t->tm_min = 0;
t->tm_hour = 0;
t->tm_isdst = -1;
midnight = mktime(t);
weekday = t->tm_wday;
while(midnight < end_time) {
temp_duration = 0;
temp_end = min(86400, end_time - midnight);
temp_start = 0;
if(start_time > midnight) {
temp_start = start_time - midnight;
}
#ifdef DEBUG
printf("Matching: %ld -> %ld. (%ld -> %ld)\n", temp_start,
temp_end, midnight + temp_start, midnight + temp_end);
#endif
/* check all time ranges for this day of the week */
for(temp_timerange = report_timeperiod->days[weekday];
temp_timerange != NULL;
temp_timerange = temp_timerange->next) {
#ifdef DEBUG
printf("Matching in timerange[%d]: %lu -> %lu (%lu -> %lu)\n",
weekday, temp_timerange->range_start,
temp_timerange->range_end, temp_start, temp_end);
#endif
start = max(temp_timerange->range_start, temp_start);
end = min(temp_timerange->range_end, temp_end);
if(start < end) {
temp_duration += end - start;
#ifdef DEBUG
printf("Matched time: %ld -> %ld = %lu\n", start, end,
temp_duration);
#endif
}
#ifdef DEBUG
else {
printf("Ignored time: %ld -> %ld\n", start, end);
}
#endif
}
state_duration += temp_duration;
temp_start = 0;
midnight += 86400;
if(++weekday > 6) weekday = 0;
}
}
/* No report timeperiod was requested; assume 24x7 */
else {
/* Calculate time in this state */
state_duration = (unsigned long)(end_time - start_time);
}
return state_duration;
}
#ifdef DEBUG
void print_availability(au_availability *availability, const char *padding) {
printf("%sAll:: up: %10lu\n", padding,
availability->time_up);
printf("%s down: %10lu\n", padding,
availability->time_down);
printf("%s unreachable: %10lu\n", padding,
availability->time_unreachable);
printf("%s ok: %10lu\n", padding,
availability->time_ok);
printf("%s warning: %10lu\n", padding,
availability->time_warning);
printf("%s critical: %10lu\n", padding,
availability->time_critical);
printf("%s unknown: %10lu\n", padding,
availability->time_unknown);
printf("%sScheduled:: up: %10lu\n", padding,
availability->scheduled_time_up);
printf("%s down: %10lu\n", padding,
availability->scheduled_time_down);
printf("%s unreachable: %10lu\n", padding,
availability->scheduled_time_unreachable);
printf("%s ok: %10lu\n", padding,
availability->scheduled_time_ok);
printf("%s warning: %10lu\n", padding,
availability->scheduled_time_warning);
printf("%s critical: %10lu\n", padding,
availability->scheduled_time_critical);
printf("%s unknown: %10lu\n", padding,
availability->scheduled_time_unknown);
printf("%sIndeterminate:: scheduled: %10lu\n", padding,
availability->scheduled_time_indeterminate);
printf("%s nodata: %10lu\n", padding,
availability->time_indeterminate_nodata);
printf("%s notrunning: %10lu\n", padding,
availability->time_indeterminate_notrunning);
}
#endif
void compute_window_availability(time_t start_time, time_t end_time,
int last_subject_state, int last_downtime_state, int last_nagios_state,
timeperiod *report_timeperiod, au_availability *availability,
int assume_state_during_nagios_downtime, int object_type,
int assume_state_retention) {
unsigned long state_duration;
#ifdef DEBUG
printf( " %lu to %lu (%lus): %s/%s/%s\n", start_time, end_time,
(end_time - start_time),
svm_get_string_from_value(last_subject_state, svm_au_states),
svm_get_string_from_value(last_downtime_state, svm_au_states),
svm_get_string_from_value(last_nagios_state, svm_au_states));
#endif
/* Get the duration of this window as adjusted for the report timeperiod */
state_duration = calculate_window_duration(start_time, end_time,
report_timeperiod);
/* Determine the appropriate state */
switch(last_nagios_state) {
case AU_STATE_PROGRAM_START:
switch(last_downtime_state) {
case AU_STATE_DOWNTIME_START:
switch(last_subject_state) {
case AU_STATE_NO_DATA:
availability->time_indeterminate_nodata += state_duration;
break;
case AU_STATE_HOST_UP:
availability->scheduled_time_up += state_duration;
break;
case AU_STATE_HOST_DOWN:
availability->scheduled_time_down += state_duration;
break;
case AU_STATE_HOST_UNREACHABLE:
availability->scheduled_time_unreachable += state_duration;
break;
case AU_STATE_SERVICE_OK:
availability->scheduled_time_ok += state_duration;
break;
case AU_STATE_SERVICE_WARNING:
availability->scheduled_time_warning += state_duration;
break;
case AU_STATE_SERVICE_UNKNOWN:
availability->scheduled_time_unknown += state_duration;
break;
case AU_STATE_SERVICE_CRITICAL:
availability->scheduled_time_critical += state_duration;
break;
}
break;
case AU_STATE_DOWNTIME_END:
switch(last_subject_state) {
case AU_STATE_NO_DATA:
availability->time_indeterminate_nodata += state_duration;
break;
case AU_STATE_HOST_UP:
availability->time_up += state_duration;
break;
case AU_STATE_HOST_DOWN:
availability->time_down += state_duration;
break;
case AU_STATE_HOST_UNREACHABLE:
availability->time_unreachable += state_duration;
break;
case AU_STATE_SERVICE_OK:
availability->time_ok += state_duration;
break;
case AU_STATE_SERVICE_WARNING:
availability->time_warning += state_duration;
break;
case AU_STATE_SERVICE_UNKNOWN:
availability->time_unknown += state_duration;
break;
case AU_STATE_SERVICE_CRITICAL:
availability->time_critical += state_duration;
break;
}
break;
}
break;
case AU_STATE_PROGRAM_END:
if(assume_state_during_nagios_downtime) {
switch(last_downtime_state) {
case AU_STATE_DOWNTIME_START:
switch(last_subject_state) {
case AU_STATE_NO_DATA:
availability->time_indeterminate_nodata += state_duration;
break;
case AU_STATE_HOST_UP:
availability->scheduled_time_up += state_duration;
break;
case AU_STATE_HOST_DOWN:
availability->scheduled_time_down += state_duration;
break;
case AU_STATE_HOST_UNREACHABLE:
availability->scheduled_time_unreachable += state_duration;
break;
case AU_STATE_SERVICE_OK:
availability->scheduled_time_ok += state_duration;
break;
case AU_STATE_SERVICE_WARNING:
availability->scheduled_time_warning += state_duration;
break;
case AU_STATE_SERVICE_UNKNOWN:
availability->scheduled_time_unknown += state_duration;
break;
case AU_STATE_SERVICE_CRITICAL:
availability->scheduled_time_critical += state_duration;
break;
}
break;
case AU_STATE_DOWNTIME_END:
switch(last_subject_state) {
case AU_STATE_NO_DATA:
availability->time_indeterminate_nodata += state_duration;
break;
case AU_STATE_HOST_UP:
availability->time_up += state_duration;
break;
case AU_STATE_HOST_DOWN:
availability->time_down += state_duration;
break;
case AU_STATE_HOST_UNREACHABLE:
availability->time_unreachable += state_duration;
break;
case AU_STATE_SERVICE_OK:
availability->time_ok += state_duration;
break;
case AU_STATE_SERVICE_WARNING:
availability->time_warning += state_duration;
break;
case AU_STATE_SERVICE_UNKNOWN:
availability->time_unknown += state_duration;
break;
case AU_STATE_SERVICE_CRITICAL:
availability->time_critical += state_duration;
break;
}
break;
}
}
else {
availability->time_indeterminate_notrunning += state_duration;
}
break;
}
#ifdef DEBUG
print_availability(availability, " ");
#endif
return;
}
static void compute_availability(au_linked_list *log_entries, time_t query_time,
time_t start_time, time_t end_time, timeperiod *report_timeperiod,
au_availability *availability, int initial_subject_state,
int assume_state_during_nagios_downtime, int object_type,
int assume_state_retention) {
int current_nagios_state;
int last_nagios_state;
int current_downtime_state;
int last_downtime_state;
int current_subject_state = initial_subject_state;
int last_subject_state = AU_STATE_NO_DATA;
au_node *temp_node;
au_log_entry *current_log_entry;
au_log_entry *last_log_entry = NULL;
au_log_alert *temp_alert_log;
au_log_downtime *temp_downtime_log;
au_log_nagios *temp_nagios_log;
time_t last_time = start_time;
time_t current_time;
#ifdef DEBUG
printf(" initial state: %s\n",
svm_get_string_from_value(initial_subject_state, valid_states));
#endif
/* Determine the initial Nagios state */
if((last_nagios_state = current_nagios_state =
get_initial_nagios_state(log_entries, start_time, end_time))
== AU_STATE_NO_DATA) {
/* This is bad; I'm giving up. */
return;
}
#ifdef DEBUG
printf(" initial nagios state: %s\n",
svm_get_string_from_value(last_nagios_state, valid_states));
#endif
/* Determine the initial downtime state */
if((last_downtime_state = current_downtime_state =
get_initial_downtime_state(log_entries, start_time, end_time))
== AU_STATE_NO_DATA) {
/* This is bad; I'm giving up. */
return;
}
#ifdef DEBUG
printf(" initial downtime state: %s\n",
svm_get_string_from_value(last_downtime_state, valid_states));
#endif
/* Process all entry pairs */
for(temp_node = log_entries->head; temp_node != NULL;
temp_node = temp_node->next) {
current_log_entry = (au_log_entry *)temp_node->data;
/* Skip everything before the start of the requested query window */
if(current_log_entry->timestamp < start_time) continue;
#ifdef DEBUG
printf(" Got log of type \"%s\" at %lu\n",
svm_get_string_from_value(current_log_entry->entry_type,
svm_au_log_types), current_log_entry->timestamp);
#endif
/* Update states */
last_subject_state = current_subject_state;
last_downtime_state = current_downtime_state;
last_nagios_state = current_nagios_state;
switch(current_log_entry->entry_type) {
case AU_LOGTYPE_ALERT:
case AU_LOGTYPE_STATE_INITIAL:
case AU_LOGTYPE_STATE_CURRENT:
temp_alert_log = (au_log_alert *)current_log_entry->entry;
current_subject_state = temp_alert_log->state;
#ifdef DEBUG
printf(" Current alert state: \"%s\"\n",
svm_get_string_from_value(current_subject_state,
valid_states));
#endif
break;
case AU_LOGTYPE_DOWNTIME:
temp_downtime_log = (au_log_downtime *)current_log_entry->entry;
current_downtime_state = temp_downtime_log->downtime_type;
#ifdef DEBUG
printf(" Current downtime state: \"%s\"\n",
svm_get_string_from_value(current_downtime_state,
valid_states));
#endif
break;
case AU_LOGTYPE_NAGIOS:
temp_nagios_log = (au_log_nagios *)current_log_entry->entry;
current_nagios_state = temp_nagios_log->type;
#ifdef DEBUG
printf(" Current nagios state: \"%s\"\n",
svm_get_string_from_value(current_nagios_state,
valid_states));
#endif
break;
default:
continue;
break;
}
/* Record the time ranges for the current log pair */
if(NULL != last_log_entry) last_time = last_log_entry->timestamp;
current_time = current_log_entry->timestamp;
/* Bail out if we've already passed the end of the requested time
range*/
if(last_time > end_time) break;
/* Clip last time if it extends beyond the end of the requested
time range */
if(current_time > end_time) current_time = end_time;
/* Clip first time if it precedes the start of the requested
time range */
if(last_time < start_time) last_time = start_time;
/* compute availability times for this chunk */
compute_window_availability(last_time, current_time, last_subject_state,
last_downtime_state, last_nagios_state, report_timeperiod,
availability, assume_state_during_nagios_downtime, object_type,
assume_state_retention);
/* Return if we've reached the end of the requested time
range */
if(current_time >= end_time) {
last_log_entry = current_log_entry;
break;
}
/* Keep track of the last item */
last_log_entry = current_log_entry;
}
/* Process the last entry */
if(last_log_entry != NULL) {
/* Update states */
last_subject_state = current_subject_state;
last_downtime_state = current_downtime_state;
last_nagios_state = current_nagios_state;
/* Don't process an entry that is beyond the end of the requested
time range */
if(last_log_entry->timestamp < end_time) {
current_time = query_time;
if(current_time > end_time) current_time = end_time;
last_time = last_log_entry->timestamp;
if(last_time < start_time) last_time = start_time;
/* compute availability times for last state */
compute_window_availability(last_time, current_time,
last_subject_state, last_downtime_state, last_nagios_state,
report_timeperiod, availability,
assume_state_during_nagios_downtime, object_type,
assume_state_retention);
}
}
else {
/* There were no log entries for the entire queried time, therefore
the whole query window was the same state */
compute_window_availability(start_time, end_time, initial_subject_state,
last_downtime_state, last_nagios_state, report_timeperiod,
availability, assume_state_during_nagios_downtime, object_type,
assume_state_retention);
}
}
void compute_host_availability(time_t query_time, time_t start_time,
time_t end_time, au_log *log, au_host *host,
timeperiod *report_timeperiod, int assume_initial_state,
int assumed_initial_host_state,
int assume_state_during_nagios_downtime, int assume_state_retention) {
hoststatus *host_status = NULL;
int last_known_state = AU_STATE_NO_DATA;
int initial_host_state = AU_STATE_NO_DATA;
#ifdef DEBUG
printf("Computing availability for host %s\n from %lu to %lu\n",
host->name, start_time, end_time);
#endif
/* If the start time is in the future, we can't compute anything */
if(start_time > query_time) return;
/* Get current host status if possible */
host_status = find_hoststatus(host->name);
/* If we don't have any data, the query time falls within the requested
query window, and we have the current host status, insert the
current state as the pseudo state at the start of the requested
query window. */
if((have_state_data(host->log_entries) == FALSE) &&
(query_time > start_time) && (query_time <= end_time) &&
(host_status != NULL)) {
switch(host_status->status) {
case HOST_DOWN:
last_known_state = AU_STATE_HOST_DOWN;
break;
case HOST_UNREACHABLE:
last_known_state = AU_STATE_HOST_UNREACHABLE;
break;
case HOST_UP:
last_known_state = AU_STATE_HOST_UP;
break;
default:
last_known_state = AU_STATE_NO_DATA;
break;
}
if(last_known_state != AU_STATE_NO_DATA) {
/* Add a dummy current state item */
#ifdef DEBUG
printf(" Inserting %s as current service pseudo-state\n",
svm_get_string_from_value(last_known_state, valid_states));
#endif
(void)au_add_alert_or_state_log(log, start_time,
AU_LOGTYPE_STATE_CURRENT, AU_OBJTYPE_HOST, host,
AU_STATETYPE_HARD, last_known_state,
"Current Host Pseudo-State");
}
}
/* Next determine the initial state of the host */
initial_host_state = get_initial_subject_state(host->log_entries,
start_time, end_time);
#ifdef DEBUG
printf(" Initial state from logs: %s\n",
svm_get_string_from_value(initial_host_state, valid_states));
#endif
if((AU_STATE_NO_DATA == initial_host_state) &&
(1 == assume_initial_state)) {
switch(assumed_initial_host_state) {
case AU_STATE_HOST_UP:
case AU_STATE_HOST_DOWN:
case AU_STATE_HOST_UNREACHABLE:
initial_host_state = assumed_initial_host_state;
break;
case AU_STATE_CURRENT_STATE:
if(host_status != NULL) {
switch(host_status->status) {
case HOST_DOWN:
initial_host_state = AU_STATE_HOST_DOWN;
break;
case HOST_UNREACHABLE:
initial_host_state = AU_STATE_HOST_UNREACHABLE;
break;
case HOST_UP:
initial_host_state = AU_STATE_HOST_UP;
break;
}
}
break;
}
}
/* At this point, if we still don't have any real data, we can't compute
anything, so return */
if((have_real_data(host->log_entries) != TRUE) &&
(AU_STATE_NO_DATA == initial_host_state)) return;
/* Compute the availability for every entry in the host's list of log
entries */
compute_availability(host->log_entries, query_time, start_time, end_time,
report_timeperiod, host->availability, initial_host_state,
assume_state_during_nagios_downtime, AU_OBJTYPE_HOST,
assume_state_retention);
}
void compute_service_availability(time_t query_time, time_t start_time,
time_t end_time, au_log *log, au_service *service,
timeperiod *report_timeperiod, int assume_initial_state,
int assumed_initial_service_state,
int assume_state_during_nagios_downtime, int assume_state_retention) {
servicestatus *service_status = NULL;
int last_known_state = AU_STATE_NO_DATA;
int initial_service_state = AU_STATE_NO_DATA;
#ifdef DEBUG
printf("Computing availability for service %s:%s\n from %lu to %lu\n",
service->host_name, service->description, start_time, end_time);
#endif
/* If the start time is in the future, we can't compute anything */
if(start_time > query_time) return;
/* Get current service status if possible */
service_status = find_servicestatus(service->host_name,
service->description);
/* If we don't have any data, the query time falls within the requested
query window, and we have the current service status, insert the
current state as the pseudo state at the start of the requested
query window. */
if((have_state_data(service->log_entries) == FALSE) &&
(query_time > start_time) && (query_time <= end_time) &&
(service_status != NULL)) {
switch(service_status->status) {
case SERVICE_OK:
last_known_state = AU_STATE_SERVICE_OK;
break;
case SERVICE_WARNING:
last_known_state = AU_STATE_SERVICE_WARNING;
break;
case SERVICE_CRITICAL:
last_known_state = AU_STATE_SERVICE_CRITICAL;
break;
case SERVICE_UNKNOWN:
last_known_state = AU_STATE_SERVICE_UNKNOWN;
break;
default:
last_known_state = AU_STATE_NO_DATA;
break;
}
if(last_known_state != AU_STATE_NO_DATA) {
/* Add a dummy current state item */
#ifdef DEBUG
printf(" Inserting %s as current service pseudo-state\n",
svm_get_string_from_value(last_known_state, valid_states));
#endif
(void)au_add_alert_or_state_log(log, start_time,
AU_LOGTYPE_STATE_CURRENT, AU_OBJTYPE_SERVICE, service,
AU_STATETYPE_HARD, last_known_state,
"Current Service Pseudo-State");
}
}
/* Next determine the initial state of the service */
initial_service_state = get_initial_subject_state(service->log_entries,
start_time, end_time);
#ifdef DEBUG
printf(" Initial state from logs: %s\n",
svm_get_string_from_value(initial_service_state, valid_states));
#endif
if((AU_STATE_NO_DATA == initial_service_state) &&
(1 == assume_initial_state)) {
switch(assumed_initial_service_state) {
case AU_STATE_SERVICE_OK:
case AU_STATE_SERVICE_WARNING:
case AU_STATE_SERVICE_UNKNOWN:
case AU_STATE_SERVICE_CRITICAL:
initial_service_state = assumed_initial_service_state;
break;
case AU_STATE_CURRENT_STATE:
if(service_status == NULL) {
switch(service_status->status) {
case SERVICE_OK:
initial_service_state = AU_STATE_SERVICE_OK;
break;
case SERVICE_WARNING:
initial_service_state = AU_STATE_SERVICE_WARNING;
break;
case SERVICE_UNKNOWN:
initial_service_state = AU_STATE_SERVICE_UNKNOWN;
break;
case SERVICE_CRITICAL:
initial_service_state = AU_STATE_SERVICE_CRITICAL;
break;
}
}
break;
}
}
/* At this point, if we still don't have any real data, we can't compute
anything, so return */
if((have_real_data(service->log_entries) != TRUE) &&
(AU_STATE_NO_DATA == initial_service_state)) return;
/* Compute the availability for every entry in the service's list of log
entries */
compute_availability(service->log_entries, query_time, start_time, end_time,
report_timeperiod, service->availability, initial_service_state,
assume_state_during_nagios_downtime, AU_OBJTYPE_SERVICE,
assume_state_retention);
}
json_object *json_archive_availability_selectors(unsigned format_options,
time_t start_time, time_t end_time, int availability_object_type,
char *host_name, char *service_description, hostgroup *hostgroup,
servicegroup *servicegroup, timeperiod *report_timeperiod,
int assume_initial_state, int assume_state_retention,
int assume_state_during_nagios_downtime,
int assumed_initial_host_state, int assumed_initial_service_state,
unsigned state_types) {
json_object *json_selectors;
json_selectors = json_new_object();
json_enumeration(json_selectors, format_options, "availabilityobjecttype",
availability_object_type, valid_availability_object_types);
if(start_time > 0) {
json_object_append_time_t(json_selectors, "starttime", start_time);
}
if(end_time > 0) {
json_object_append_time_t(json_selectors, "endtime", end_time);
}
if(NULL != host_name) {
json_object_append_string(json_selectors, "hostname", &percent_escapes,
host_name);
}
if(NULL != service_description) {
json_object_append_string(json_selectors, "servicedescription",
&percent_escapes, service_description);
}
if(NULL != hostgroup) {
json_object_append_string(json_selectors, "hostgroup", &percent_escapes,
hostgroup->group_name);
}
if(NULL != servicegroup) {
json_object_append_string(json_selectors, "servicegroup",
&percent_escapes, servicegroup->group_name);
}
if(NULL != report_timeperiod) {
json_object_append_string(json_selectors, "timeperiod",
&percent_escapes, report_timeperiod->name);
}
json_object_append_boolean(json_selectors, "assumeinitialstate",
assume_initial_state);
json_object_append_boolean(json_selectors, "assumestateretention",
assume_state_retention);
json_object_append_boolean(json_selectors,
"assumestateduringnagiosdowntime",
assume_state_during_nagios_downtime);
if(assumed_initial_host_state != AU_STATE_NO_DATA) {
json_enumeration(json_selectors, format_options,
"assumedinitialhoststate", assumed_initial_host_state,
valid_initial_host_states);
}
if(assumed_initial_service_state != AU_STATE_NO_DATA) {
json_enumeration(json_selectors, format_options,
"assumedinitialservicestate", assumed_initial_service_state,
valid_initial_service_states);
}
if(state_types != AU_STATETYPE_ALL) {
json_bitmask(json_selectors, format_options, "statetypes", state_types,
valid_state_types);
}
return json_selectors;
}
json_object *json_archive_availability(unsigned format_options,
time_t query_time, time_t start_time, time_t end_time,
int availability_object_type, char *host_name,
char *service_description, hostgroup *hostgroup_selector,
servicegroup *servicegroup_selector, timeperiod *report_timeperiod,
int assume_initial_state, int assume_state_retention,
int assume_state_during_nagios_downtime,
int assumed_initial_host_state, int assumed_initial_service_state,
unsigned state_types, au_log *log) {
json_object *json_data;
host *temp_host;
json_array *json_host_list;
json_object *json_host_object;
service *temp_service;
json_array *json_service_list;
json_object *json_service_object;
hostgroup *temp_hostgroup;
json_object *json_hostgroup_object;
json_array *json_hostgroup_list;
hostsmember *temp_hostgroup_member;
servicegroup *temp_servicegroup;
json_object *json_servicegroup_object;
json_array *json_servicegroup_list;
servicesmember *temp_servicegroup_member;
json_data = json_new_object();
json_object_append_object(json_data, "selectors",
json_archive_availability_selectors(format_options, start_time,
end_time, availability_object_type, host_name, service_description,
hostgroup_selector, servicegroup_selector, report_timeperiod,
assume_initial_state, assume_state_retention,
assume_state_during_nagios_downtime, assumed_initial_host_state,
assumed_initial_service_state, state_types));
/* First compute the availability */
switch(availability_object_type) {
case AU_OBJTYPE_HOST:
if(NULL == host_name) { /* compute for all hosts */
json_host_list = json_new_array();
for(temp_host = host_list; temp_host != NULL;
temp_host = temp_host->next) {
if(FALSE == is_authorized_for_host(temp_host,
¤t_authdata)) {
continue;
}
json_host_object =
json_archive_single_host_availability(
format_options, query_time, start_time, end_time,
temp_host->name, report_timeperiod,
assume_initial_state, assume_state_retention,
assume_state_during_nagios_downtime,
assumed_initial_host_state, state_types, log);
if(NULL != json_host_object) {
json_array_append_object(json_host_list,
json_host_object);
}
}
json_object_append_array(json_data, "hosts", json_host_list);
}
else {
temp_host = find_host(host_name);
if((NULL != temp_host) &&
(TRUE == is_authorized_for_host(temp_host,
¤t_authdata))) {
json_host_object =
json_archive_single_host_availability(format_options,
query_time, start_time, end_time, host_name,
report_timeperiod, assume_initial_state,
assume_state_retention,
assume_state_during_nagios_downtime,
assumed_initial_host_state, state_types, log);
if(NULL != json_host_object) {
json_object_append_object(json_data, "host",
json_host_object);
}
}
}
break;
case AU_OBJTYPE_SERVICE:
if((NULL == host_name) && (NULL == service_description)) {
/* compute for all services on all hosts */
json_service_list = json_new_array();
for(temp_service = service_list; temp_service != NULL;
temp_service = temp_service->next) {
if(FALSE == is_authorized_for_service(temp_service,
¤t_authdata)) {
continue;
}
json_service_object =
json_archive_single_service_availability(
format_options, query_time, start_time, end_time,
temp_service->host_name, temp_service->description,
report_timeperiod, assume_initial_state,
assume_state_retention,
assume_state_during_nagios_downtime,
assumed_initial_service_state, state_types, log);
if(NULL != json_service_object) {
json_array_append_object(json_service_list,
json_service_object);
}
}
json_object_append_array(json_data, "services",
json_service_list);
}
else if(NULL == service_description) {
/* compute for all services on the specified host */
json_service_list = json_new_array();
for(temp_service = service_list; temp_service != NULL;
temp_service = temp_service->next) {
if(!strcmp(temp_service->host_name, host_name) &&
(TRUE == is_authorized_for_service(temp_service,
¤t_authdata))) {
json_service_object =
json_archive_single_service_availability(
format_options, query_time, start_time,
end_time, temp_service->host_name,
temp_service->description, report_timeperiod,
assume_initial_state, assume_state_retention,
assume_state_during_nagios_downtime,
assumed_initial_service_state, state_types,
log);
if(NULL != json_service_object) {
json_array_append_object(json_service_list,
json_service_object);
}
}
}
json_object_append_array(json_data, "services",
json_service_list);
}
else if(NULL == host_name) {
/* compute for all services on the specified host */
json_service_list = json_new_array();
for(temp_service = service_list; temp_service != NULL;
temp_service = temp_service->next) {
if(!strcmp(temp_service->description,
service_description) &&
(TRUE == is_authorized_for_service(temp_service,
¤t_authdata))) {
json_service_object =
json_archive_single_service_availability(
format_options, query_time, start_time,
end_time, temp_service->host_name,
temp_service->description, report_timeperiod,
assume_initial_state, assume_state_retention,
assume_state_during_nagios_downtime,
assumed_initial_service_state, state_types,
log);
if(NULL != json_service_object) {
json_array_append_object(json_service_list,
json_service_object);
}
}
}
json_object_append_array(json_data, "services",
json_service_list);
}
else {
temp_service = find_service(host_name, service_description);
if((NULL != temp_service) &&
(TRUE == is_authorized_for_service(temp_service,
¤t_authdata))) {
json_service_object =
json_archive_single_service_availability(format_options,
query_time, start_time, end_time, host_name,
service_description, report_timeperiod,
assume_initial_state, assume_state_retention,
assume_state_during_nagios_downtime,
assumed_initial_service_state, state_types, log);
if(NULL != json_service_object) {
json_object_append_object(json_data, "service",
json_service_object);
}
}
}
break;
case AU_OBJTYPE_HOSTGROUP:
if(NULL == hostgroup_selector) {
/* compute for all hosts in all hostgroups */
json_hostgroup_list = json_new_array();
for(temp_hostgroup = hostgroup_list; temp_hostgroup != NULL;
temp_hostgroup = temp_hostgroup->next) {
json_host_list = json_new_array();
for(temp_hostgroup_member = temp_hostgroup->members;
temp_hostgroup_member != NULL;
temp_hostgroup_member =
temp_hostgroup_member->next) {
if(FALSE == is_authorized_for_host(temp_hostgroup_member->host_ptr,
¤t_authdata)) {
continue;
}
json_host_object =
json_archive_single_host_availability(
format_options, query_time, start_time,
end_time, temp_hostgroup_member->host_name,
report_timeperiod, assume_initial_state,
assume_state_retention,
assume_state_during_nagios_downtime,
assumed_initial_host_state, state_types, log);
if(NULL != json_host_object) {
json_array_append_object(json_host_list,
json_host_object);
}
}
json_hostgroup_object = json_new_object();
json_object_append_string(json_hostgroup_object, "name",
&percent_escapes, temp_hostgroup->group_name);
json_object_append_array(json_hostgroup_object, "hosts",
json_host_list);
json_array_append_object(json_hostgroup_list,
json_hostgroup_object);
}
json_object_append_array(json_data, "hostgroups",
json_hostgroup_list);
}
else {
/* compute for all hosts in the specified hostgroup */
json_host_list = json_new_array();
for(temp_hostgroup_member = hostgroup_selector->members;
temp_hostgroup_member != NULL;
temp_hostgroup_member = temp_hostgroup_member->next) {
if(FALSE == is_authorized_for_host(temp_hostgroup_member->host_ptr,
¤t_authdata)) {
continue;
}
json_host_object =
json_archive_single_host_availability(
format_options, query_time, start_time, end_time,
temp_hostgroup_member->host_name, report_timeperiod,
assume_initial_state, assume_state_retention,
assume_state_during_nagios_downtime,
assumed_initial_host_state, state_types, log);
if(NULL != json_host_object) {
json_array_append_object(json_host_list,
json_host_object);
}
}
json_hostgroup_object = json_new_object();
json_object_append_string(json_hostgroup_object, "name",
&percent_escapes, hostgroup_selector->group_name);
json_object_append_array(json_hostgroup_object, "hosts",
json_host_list);
json_object_append_object(json_data, "hostgroup",
json_hostgroup_object);
}
break;
case AU_OBJTYPE_SERVICEGROUP:
if(NULL == servicegroup_selector) {
/* compute for all hosts in all hostgroups */
json_servicegroup_list = json_new_array();
for(temp_servicegroup = servicegroup_list;
temp_servicegroup != NULL;
temp_servicegroup = temp_servicegroup->next) {
json_service_list = json_new_array();
for(temp_servicegroup_member = temp_servicegroup->members;
temp_servicegroup_member != NULL;
temp_servicegroup_member =
temp_servicegroup_member->next) {
if(FALSE == is_authorized_for_service(temp_servicegroup_member->service_ptr,
¤t_authdata)) {
continue;
}
json_service_object =
json_archive_single_service_availability(
format_options, query_time, start_time,
end_time, temp_servicegroup_member->host_name,
temp_servicegroup_member->service_description,
report_timeperiod, assume_initial_state,
assume_state_retention,
assume_state_during_nagios_downtime,
assumed_initial_service_state, state_types,
log);
if(NULL != json_service_object) {
json_array_append_object(json_service_list,
json_service_object);
}
}
json_servicegroup_object = json_new_object();
json_object_append_string(json_servicegroup_object, "name",
&percent_escapes, temp_servicegroup->group_name);
json_object_append_array(json_servicegroup_object, "hosts",
json_service_list);
json_array_append_object(json_servicegroup_list,
json_servicegroup_object);
}
json_object_append_array(json_data, "servicegroups",
json_servicegroup_list);
}
else {
/* compute for all services in the specified servicegroup */
json_service_list = json_new_array();
for(temp_servicegroup_member = servicegroup_selector->members;
temp_servicegroup_member != NULL;
temp_servicegroup_member =
temp_servicegroup_member->next) {
if(FALSE == is_authorized_for_service(temp_servicegroup_member->service_ptr,
¤t_authdata)) {
continue;
}
json_service_object =
json_archive_single_service_availability(
format_options, query_time, start_time, end_time,
temp_servicegroup_member->host_name,
temp_servicegroup_member->service_description,
report_timeperiod, assume_initial_state,
assume_state_retention,
assume_state_during_nagios_downtime,
assumed_initial_service_state, state_types, log);
if(NULL != json_service_object) {
json_array_append_object(json_service_list,
json_service_object);
}
}
json_servicegroup_object = json_new_object();
json_object_append_string(json_servicegroup_object, "name",
&percent_escapes, servicegroup_selector->group_name);
json_object_append_array(json_servicegroup_object, "services",
json_service_list);
json_object_append_object(json_data, "servicegroup",
json_servicegroup_object);
}
break;
default:
break;
}
return json_data;
}
json_object *json_archive_single_host_availability(unsigned format_options,
time_t query_time, time_t start_time, time_t end_time, char *host_name,
timeperiod *report_timeperiod, int assume_initial_state,
int assume_state_retention, int assume_state_during_nagios_downtime,
int assumed_initial_host_state, unsigned state_types, au_log *log) {
au_host *host = NULL;
au_node *temp_entry = NULL;
au_host *global_host = NULL;
json_object *json_host_availability = NULL;
host = au_find_host(log->hosts, host_name);
if(NULL == host) {
/* host has no log entries, so create the host */
host = au_add_host(log->hosts, host_name);
/* Add global events to this new host */
global_host = au_find_host(log->hosts, "*");
if(NULL != global_host) {
for(temp_entry = global_host->log_entries->head; NULL != temp_entry;
temp_entry = temp_entry->next) {
if(au_list_add_node(host->log_entries, temp_entry->data,
au_cmp_log_entries) == 0) {
break;
}
}
}
}
if((host->availability = calloc(1, sizeof(au_availability))) != NULL) {
compute_host_availability(query_time, start_time, end_time, log, host,
report_timeperiod, assume_initial_state,
assumed_initial_host_state,
assume_state_during_nagios_downtime, assume_state_retention);
json_host_availability = json_archive_host_availability(format_options,
host->name, host->availability);
}
return json_host_availability;
}
json_object *json_archive_single_service_availability(unsigned format_options,
time_t query_time, time_t start_time, time_t end_time, char *host_name,
char *service_description, timeperiod *report_timeperiod,
int assume_initial_state, int assume_state_retention,
int assume_state_during_nagios_downtime,
int assumed_initial_service_state, unsigned state_types, au_log *log) {
au_service *service = NULL;
au_node *temp_entry = NULL;
au_host *global_host = NULL;
json_object *json_service_availability = NULL;
service = au_find_service(log->services, host_name, service_description);
if(NULL == service) {
/* service has no log entries, so create the service */
service = au_add_service(log->services, host_name, service_description);
/* Add global events to this new service */
global_host = au_find_host(log->hosts, "*");
if(NULL != global_host) {
for(temp_entry = global_host->log_entries->head; NULL != temp_entry;
temp_entry = temp_entry->next) {
if(au_list_add_node(service->log_entries, temp_entry->data,
au_cmp_log_entries) == 0) {
break;
}
}
}
}
if((service->availability = calloc(1, sizeof(au_availability))) != NULL) {
compute_service_availability(query_time, start_time, end_time, log,
service, report_timeperiod, assume_initial_state,
assumed_initial_service_state,
assume_state_during_nagios_downtime, assume_state_retention);
json_service_availability =
json_archive_service_availability(format_options,
service->host_name, service->description,
service->availability);
}
return json_service_availability;
}
json_object *json_archive_host_availability(unsigned format_options,
char *name, au_availability *availability) {
json_object *json_host_availability;
json_host_availability = json_new_object();
if(name != NULL) {
json_object_append_string(json_host_availability, "name",
&percent_escapes, name);
}
json_object_append_duration(json_host_availability, "time_up",
availability->time_up);
json_object_append_duration(json_host_availability, "time_down",
availability->time_down);
json_object_append_duration(json_host_availability, "time_unreachable",
availability->time_unreachable);
json_object_append_duration(json_host_availability, "scheduled_time_up",
availability->scheduled_time_up);
json_object_append_duration(json_host_availability, "scheduled_time_down",
availability->scheduled_time_down);
json_object_append_duration(json_host_availability,
"scheduled_time_unreachable",
availability->scheduled_time_unreachable);
json_object_append_duration(json_host_availability,
"time_indeterminate_nodata",
availability->time_indeterminate_nodata);
json_object_append_duration(json_host_availability,
"time_indeterminate_notrunning",
availability->time_indeterminate_notrunning);
return json_host_availability;
}
json_object *json_archive_service_availability(unsigned format_options,
char *host_name, char *description, au_availability *availability) {
json_object *json_service_availability;
json_service_availability = json_new_object();
if(host_name != NULL) {
json_object_append_string(json_service_availability, "host_name",
&percent_escapes, host_name);
}
if(description != NULL) {
json_object_append_string(json_service_availability, "description",
&percent_escapes, description);
}
json_object_append_duration(json_service_availability, "time_ok",
availability->time_ok);
json_object_append_duration(json_service_availability, "time_warning",
availability->time_warning);
json_object_append_duration(json_service_availability, "time_critical",
availability->time_critical);
json_object_append_duration(json_service_availability, "time_unknown",
availability->time_unknown);
json_object_append_duration(json_service_availability, "scheduled_time_ok",
availability->scheduled_time_ok);
json_object_append_duration(json_service_availability,
"scheduled_time_warning", availability->scheduled_time_warning);
json_object_append_duration(json_service_availability,
"scheduled_time_critical", availability->scheduled_time_critical);
json_object_append_duration(json_service_availability,
"scheduled_time_unknown", availability->scheduled_time_unknown);
json_object_append_duration(json_service_availability,
"time_indeterminate_nodata",
availability->time_indeterminate_nodata);
json_object_append_duration(json_service_availability,
"time_indeterminate_notrunning",
availability->time_indeterminate_notrunning);
return json_service_availability;
}
|