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
|
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2024 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
function run_data_query($host_id, $snmp_query_id, $automation = false, $force = false) {
global $config, $input_types;
if (read_config_option('data_source_trace') == 'on') {
cacti_log("Running Re-Index for Device[$host_id], DQ[$snmp_query_id]", false, 'DSTRACE');
}
/* required for upgrading old versions of cacti */
if (!db_column_exists('host', 'poller_id')) {
return false;
}
/* don't run/rerun the query if the host is down, or disabled */
$status = db_fetch_row_prepared('SELECT status, disabled, poller_id
FROM host
WHERE id = ?',
array($host_id));
if (!cacti_sizeof($status)) {
return false;
} elseif ($status['status'] == HOST_DOWN || $status['disabled'] == 'on') {
return true;
}
$poller_id = $status['poller_id'];
if ($poller_id != $config['poller_id']) {
$hostname = db_fetch_cell_prepared('SELECT hostname
FROM poller
WHERE id = ?',
array($poller_id));
$url = $config['url_path'] . 'remote_agent.php?action=runquery&host_id=' . $host_id . '&data_query_id=' . $snmp_query_id;
$response = call_remote_data_collector($poller_id, $url);
if ($response != '') {
$response = json_decode($response, true);
$_SESSION['debug_log']['data_query'] = $response['data_query'];
if (isset($_SESSION['debug_log']['response'])) {
$result = $_SESSION['debug_log']['response'];
unset($_SESSION['debug_log']['response']);
automation_execute_data_query($host_id, $snmp_query_id);
query_debug_timer_offset('data_query', __esc('Automation Execution for Data Query complete'));
api_plugin_hook_function('run_data_query', array('host_id' => $host_id, 'snmp_query_id' => $snmp_query_id));
query_debug_timer_offset('data_query', __esc('Plugin Hooks complete'));
return $result;
}
}
return false;
}
include_once($config['library_path'] . '/poller.php');
include_once($config['library_path'] . '/api_data_source.php');
include_once($config['library_path'] . '/utility.php');
query_debug_timer_start();
// Load the XML structure for custom settings detection
$query_array = get_data_query_array($snmp_query_id);
query_debug_timer_offset('data_query', __esc('Running Data Query [%s].', $snmp_query_id));
$type_id = db_fetch_cell_prepared('SELECT data_input.type_id
FROM snmp_query
INNER JOIN data_input
ON snmp_query.data_input_id = data_input.id
WHERE snmp_query.id = ?',
array($snmp_query_id));
if (isset($input_types[$type_id])) {
query_debug_timer_offset('data_query', __esc('Found Type = \'%s\' [%s].', $type_id, $input_types[$type_id]));
}
if ($type_id == DATA_INPUT_TYPE_SNMP_QUERY) {
$result = query_snmp_host($host_id, $snmp_query_id);
} elseif ($type_id == DATA_INPUT_TYPE_SCRIPT_QUERY) {
$result = query_script_host($host_id, $snmp_query_id);
} elseif ($type_id == DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER) {
$result = query_script_host($host_id, $snmp_query_id);
} else {
$result = false;
$arguments = array(
'result' => $result,
'host_id' => $host_id,
'snmp_query_id' => $snmp_query_id
);
$arguments = api_plugin_hook_function('run_data_query', $arguments);
if (isset($arguments['result']) && $arguments['result'] !== false) {
$result = $arguments['result'];
} else {
query_debug_timer_offset('data_query', __esc('Unknown Type = \'%s\'.', $type_id));
}
}
// If re-indexing fails, don't continue
if (!$result) {
return false;
}
// SNMP Queries can specify the index as a 'source' column
if (!isset($query_array['index_order'])) {
$index_is_source = true;
} else {
$index_is_source = false;
}
$old_sort_field = get_best_data_query_index_type($host_id, $snmp_query_id);
/* update the sort cache if there is a sort field */
if ($index_is_source == false) {
$new_sort_field = update_data_query_sort_cache($host_id, $snmp_query_id);
} else {
$new_sort_field = $old_sort_field;
}
if ($new_sort_field === false) {
cacti_log('ERROR: Re-Indexing failed due to a NULL sort field for Device[' . $host_id . '] and DQ[' . $snmp_query_id . ']. Can not continue with Re-Index.', false, 'REINDEX');
return false;
}
$remap = false;
if ($index_is_source == false) {
if (query_check_suitable($new_sort_field, $old_sort_field, $host_id, $snmp_query_id)) {
if ($old_sort_field != $new_sort_field) {
if ($old_sort_field != '') {
query_debug_timer_offset('data_query', __esc('WARNING: Sort Field Association has Changed. Re-mapping issues may occur!'));
cacti_log('WARNING: Sort Field has Changed for Device[' . $host_id . '] and DQ[' . $snmp_query_id . ']. Old Sort:' . $old_sort_field . ', New Sort:' . $new_sort_field . '. Re-mapping issues may occur!', false, 'REINDEX');
}
$remap = true;
}
} else {
$new_sort_field = $old_sort_field;
}
}
query_debug_timer_offset('data_query', __esc('Update Data Query Sort Cache complete'));
/* recalculate/change sort order */
$local_data = db_fetch_assoc_prepared('SELECT dl.id AS local_data_id, dl.host_id,
dl.snmp_query_id, dl.snmp_index, dl.orphan,
did.data_input_field_id, did.data_template_data_id,
did.value AS query_index, "' . $old_sort_field . '" AS sort_field
FROM data_local AS dl
INNER JOIN data_template_data AS dtd
ON dl.id = dtd.local_data_id
INNER JOIN data_input_fields AS dif
ON dtd.data_input_id = dif.data_input_id
LEFT JOIN data_input_data AS did
ON dtd.id = did.data_template_data_id
AND dif.id = did.data_input_field_id
WHERE dif.data_name="index_value"
AND dl.snmp_query_id = ?
AND dl.host_id = ?',
array($snmp_query_id, $host_id));
$changed_ids = array();
$orphaned_ids = array();
$push_out = array();
if (cacti_sizeof($local_data)) {
query_debug_timer_offset('data_query', __esc('Found %s Local Data ID\'s to Verify', cacti_sizeof($local_data)));
foreach($local_data as $data_source) {
// Just in case there is a forced type from the data source page
$forced_type = false;
// Check to see if the user had manually set the index type
$previous_sort_field = $old_sort_field;
$previous_did = db_fetch_row_prepared('SELECT did.*
FROM data_local AS dl
INNER JOIN data_template_data AS dtd
ON dl.id = dtd.local_data_id
INNER JOIN data_input_fields AS dif
ON dtd.data_input_id = dif.data_input_id
LEFT JOIN data_input_data AS did
ON dtd.id = did.data_template_data_id
AND dif.id = did.data_input_field_id
WHERE dif.data_name="index_type"
AND dl.id = ?',
array($data_source['local_data_id']));
if (cacti_sizeof($previous_did)) {
if ($previous_did['value'] != $previous_sort_field) {
$forced_type = true;
$previous_sort_field = $previous_did['value'];
}
}
if (!$forced_type) {
// See if the index is in the host cache
$current_index = db_fetch_cell_prepared('SELECT snmp_index
FROM host_snmp_cache
WHERE host_id = ?
AND snmp_query_id = ?
AND field_name = ?
AND field_value = ?',
array($host_id, $snmp_query_id, $data_source['sort_field'], $data_source['query_index']));
} else {
$current_value = db_fetch_cell_prepared('SELECT value
FROM data_input_data AS did
INNER JOIN data_input_fields AS dif
ON did.data_input_field_id=dif.id
INNER JOIN data_template_data AS dtd
ON dtd.id=did.data_template_data_id
INNER JOIN data_local AS dl
ON dl.id=dtd.local_data_id
WHERE dl.id = ?
AND dl.snmp_query_id = ?
AND data_name="index_value"',
array($data_source['local_data_id'], $snmp_query_id));
$current_index = db_fetch_cell_prepared('SELECT snmp_index
FROM host_snmp_cache
WHERE host_id = ?
AND snmp_query_id = ?
AND field_name = ?
AND field_value = ?',
array($host_id, $snmp_query_id, $previous_sort_field, $current_value));
}
if ($remap) {
if ($new_sort_field != $previous_sort_field) {
$new_field_value = db_fetch_cell_prepared('SELECT field_value
FROM host_snmp_cache
WHERE host_id = ?
AND snmp_query_id = ?
AND field_name = ?
AND snmp_index = ?',
array($host_id, $snmp_query_id, $new_sort_field, $current_index));
$did_map_data = db_fetch_row_prepared('SELECT value, data_input_field_id, data_template_data_id,
host_id, snmp_query_id
FROM data_input_data AS did
INNER JOIN data_input_fields AS dif
ON did.data_input_field_id=dif.id
INNER JOIN data_template_data AS dtd
ON dtd.id=did.data_template_data_id
INNER JOIN data_local AS dl
ON dl.id=dtd.local_data_id
WHERE value = ?
AND dl.id = ?
AND dl.snmp_query_id = ?
AND data_name="index_type"',
array($previous_sort_field, $data_source['local_data_id'], $snmp_query_id));
} else {
$remap = false;
}
}
if ($current_index != '') {
// Non blank index found
// Check to see if the index changed
if ($current_index != $data_source['snmp_index']) {
query_debug_timer_offset('data_query', __esc('Index Change Detected! CurrentIndex: %s, PreviousIndex: %s', $current_index, $data_source['query_index']));
db_execute_prepared('UPDATE data_local
SET snmp_index = ?,
orphan = 0
WHERE id = ?',
array($current_index, $data_source['local_data_id']));
$changed_ids[] = $data_source['local_data_id'];
} elseif ($data_source['orphan'] == 1) {
db_execute_prepared('UPDATE data_local
SET orphan = 0
WHERE id = ?',
array($data_source['local_data_id']));
$push_out[$data_source['local_data_id']] = $data_source['local_data_id'];
}
} elseif ($data_source['snmp_index'] != '' && !$forced_type) {
if (isset($query_array['index_transient']) && $query_array['index_transient'] == 'true') {
// Found removed index, but this is expected, so no action taken
query_debug_timer_offset('data_query', __esc('Transient Index Removal Detected! PreviousIndex: %s. No action taken.', $data_source['query_index']));
} else {
// Found a deleted index, masking off to prevent issues
query_debug_timer_offset('data_query', __esc('Index Removal Detected! PreviousIndex: %s', $data_source['query_index']));
// Set the index to Null, note that the Data Source still has the value
db_execute_prepared('UPDATE data_local
SET orphan = 1
WHERE id = ?',
array($data_source['local_data_id']));
$orphaned_ids[] = $data_source['local_data_id'];
}
}
if ($remap && trim($new_field_value) != '' && $new_sort_field != '') {
db_execute_prepared('UPDATE data_input_data
SET value = ?
WHERE data_input_field_id = ?
AND data_template_data_id = ?',
array($new_field_value, $data_source['data_input_field_id'], $data_source['data_template_data_id']));
db_execute_prepared('UPDATE data_input_data
SET value = ?
WHERE data_input_field_id = ?
AND data_template_data_id = ?',
array($new_sort_field, $previous_did['data_input_field_id'], $previous_did['data_template_data_id']));
}
}
/* if we've resolved orphan issues, push out the data source */
if (cacti_sizeof($push_out)) {
foreach($push_out as $po_local_data_id) {
push_out_host($host_id, $po_local_data_id);
}
}
query_debug_timer_offset('data_query', __esc('Verification of %s Local Data ID\'s Complete', cacti_sizeof($local_data)));
if (cacti_sizeof($changed_ids) || cacti_sizeof($orphaned_ids)) {
query_debug_timer_offset('data_query', __esc('Found Changed %s and %s Orphaned Local Data ID\'s to Re-map.', cacti_sizeof($changed_ids), cacti_sizeof($orphaned_ids)));
data_query_remap_indexes($changed_ids);
data_query_remap_indexes($orphaned_ids);
query_debug_timer_offset('data_query', __esc('Done remapping Graphs to their new Indexes'));
}
if ((cacti_sizeof($changed_ids) || cacti_sizeof($orphaned_ids)) && !$force) {
/* update title cache for graph and data source */
update_data_source_title_cache_from_host($host_id, $snmp_query_id, array_merge($changed_ids, $orphaned_ids));
query_debug_timer_offset('data_query', __esc('Done updating Data Source Title Cache'));
update_graph_title_cache_from_host($host_id, $snmp_query_id, array_merge($changed_ids, $orphaned_ids));
query_debug_timer_offset('data_query', __esc('Done updating Graph Title Cache'));
} elseif ($force) {
/* update title cache for graph and data source */
update_data_source_title_cache_from_host($host_id, $snmp_query_id);
query_debug_timer_offset('data_query', __esc('Done updating Data Source Title Cache'));
update_graph_title_cache_from_host($host_id, $snmp_query_id);
query_debug_timer_offset('data_query', __esc('Done updating Graph Title Cache'));
}
}
query_debug_timer_offset('data_query', __esc('Index Association with Local Data complete'));
update_reindex_cache($host_id, $snmp_query_id);
/* update the auto reindex cache */
if (cacti_sizeof($changed_ids)) {
query_debug_timer_offset('data_query', __esc('Update Re-Index Cache complete. There were %s index changes, and %s orphaned indexes.', cacti_sizeof($changed_ids), cacti_sizeof($orphaned_ids)));
/* update the poller cache */
update_poller_cache_from_query($host_id, $snmp_query_id, $changed_ids);
query_debug_timer_offset('data_query', __esc('Update Poller Cache for Query complete'));
} else {
query_debug_timer_offset('data_query', __esc('No Index Changes Detected, Skipping Re-Index and Poller Cache Re-population'));
$existing_orphans = db_fetch_cell_prepared('SELECT COUNT(*)
FROM data_local
WHERE snmp_query_id = ?
AND host_id = ?
AND orphan = 1',
array($snmp_query_id, $host_id));
if ($existing_orphans) {
$data_template_id = db_fetch_cell_prepared('SELECT data_template_id
FROM data_local
WHERE snmp_query_id = ?
LIMIT 1',
array($snmp_query_id));
db_execute_prepared('UPDATE data_local
SET orphan = 0
WHERE snmp_query_id = ?
AND host_id = ?',
array($snmp_query_id, $host_id));
if (isset($query_array['index_orphan_removal']) && $query_array['index_orphan_removal'] == 'true') {
push_out_host($host_id, 0, $data_template_id);
}
}
}
if (cacti_sizeof($orphaned_ids) &&
(isset($query_array['index_orphan_removal']) && $query_array['index_orphan_removal'] == 'true')) {
$poller_ids = array_rekey(
db_fetch_assoc_prepared('SELECT DISTINCT poller_id
FROM poller_item
WHERE local_data_id IN (' . implode(', ', $orphaned_ids) . ')'),
'poller_id', 'poller_id'
);
if (cacti_sizeof($poller_ids)) {
foreach ($poller_ids as $poller_id) {
api_data_source_cache_crc_update($poller_id);
}
}
}
if ($config['poller_id'] == 1) {
/* perform any automation on reindex */
automation_execute_data_query($host_id, $snmp_query_id);
query_debug_timer_offset('data_query', __esc('Automation Executing for Data Query complete'));
api_plugin_hook_function('run_data_query', array('host_id' => $host_id, 'snmp_query_id' => $snmp_query_id));
query_debug_timer_offset('data_query', __esc('Plugin hooks complete'));
} elseif ($config['connection'] == 'online') {
poller_push_reindex_data_to_poller($host_id, $snmp_query_id);
automation_execute_data_query($host_id, $snmp_query_id);
query_debug_timer_offset('data_query', __esc('Automation Execution for Data Query complete'));
api_plugin_hook_function('run_data_query', array('host_id' => $host_id, 'snmp_query_id' => $snmp_query_id));
query_debug_timer_offset('data_query', __esc('Plugin Hooks complete'));
if (!isset($_SESSION)) {
$config['debug_log']['result'] = $result;
print json_encode($config['debug_log']);
} else {
$_SESSION['debug_log']['result'] = $result;
print json_encode($_SESSION['debug_log']);
kill_session_var('debug_log');
}
}
// Remove orphans only if the data query expressly calls for it
if (cacti_sizeof($orphaned_ids) &&
(isset($query_array['index_orphan_removal']) && $query_array['index_orphan_removal'] == 'true')) {
data_query_remove_disabled_items($orphaned_ids);
}
return (isset($result) ? $result : true);
}
function data_query_remove_disabled_items($orphaned_ids) {
if (cacti_sizeof($orphaned_ids)) {
db_execute_prepared('DELETE FROM poller_item
WHERE local_data_id IN (' . implode(', ', $orphaned_ids) . ')');
db_execute_prepared('DELETE FROM poller_output_boost
WHERE local_data_id IN (' . implode(', ', $orphaned_ids) . ')');
$archive_tables = array_rekey(
db_fetch_assoc('SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_NAME LIKE "poller_output_boost_arch%"'),
'TABLE_NAME', 'TABLE_NAME'
);
if (cacti_sizeof($archive_tables)) {
foreach($archive_tables as $table) {
db_execute_prepared("DELETE FROM $table
WHERE local_data_id IN (" . implode(', ', $orphaned_ids) . ')', false);
}
}
}
}
function query_check_suitable($new_sort_field, $old_sort_field, $host_id, $snmp_query_id) {
if ($new_sort_field == $old_sort_field) {
query_debug_timer_offset('data_query', __esc('Checking for Sort Field change. No changes detected.'));
return true;
}
query_debug_timer_offset('data_query', __esc('Detected New Sort Field: \'%s\' Old Sort Field \'%s\'', $new_sort_field, $old_sort_field));
$new_sort_count = db_fetch_cell_prepared('SELECT COUNT(*)
FROM host_snmp_cache
WHERE host_id = ?
AND snmp_query_id = ?
AND field_name = ?',
array($host_id, $snmp_query_id, $new_sort_field));
$old_sort_count = db_fetch_cell_prepared('SELECT COUNT(*)
FROM host_snmp_cache
WHERE host_id = ?
AND snmp_query_id = ?
AND field_name = ?',
array($host_id, $snmp_query_id, $old_sort_field));
if ($new_sort_count < $old_sort_count) {
query_debug_timer_offset('data_query', __esc('ERROR: New Sort Field not suitable. Sort Field will not change.'));
/* update the cache */
db_execute_prepared('UPDATE host_snmp_query
SET sort_field = ?
WHERE host_id = ?
AND snmp_query_id = ?',
array($old_sort_field, $host_id, $snmp_query_id));
return false;
}
query_debug_timer_offset('data_query', __esc('New Sort Field validated. Sort Field be updated.'));
return true;
}
function data_query_remap_indexes($local_data) {
if (cacti_sizeof($local_data)) {
foreach($local_data as $id) {
$index = db_fetch_cell_prepared('SELECT snmp_index
FROM data_local
WHERE id = ?',
array($id));
$local_graph_ids = array_rekey(
db_fetch_assoc_prepared('SELECT DISTINCT gti.local_graph_id
FROM graph_templates_item AS gti
INNER JOIN data_template_rrd AS dtr
ON gti.task_item_id=dtr.id
WHERE dtr.local_data_id = ?',
array($id)),
'local_graph_id', 'local_graph_id'
);
if (cacti_sizeof($local_graph_ids)) {
db_execute_prepared('UPDATE graph_local
SET snmp_index = ?
WHERE id IN(' . implode(', ', $local_graph_ids) . ')',
array($index));
}
}
}
}
function data_query_update_input_method($snmp_query_id, $previous_input_id, $new_input_id = '') {
$change_data_input = false;
if ($previous_input_id != $new_input_id) {
$change_data_input = true;
}
$data_templates = array_rekey(
db_fetch_assoc_prepared('SELECT DISTINCT data_template_id
FROM data_local
WHERE snmp_query_id = ?',
array($snmp_query_id)),
'data_template_id', 'data_template_id'
);
if (cacti_sizeof($data_templates)) {
// Look for messed up templates
$data_inputs = db_fetch_assoc('SELECT DISTINCT data_input_id
FROM data_template_data
WHERE data_template_id IN (' . implode(', ', array_keys($data_templates)) . ')');
if (cacti_sizeof($data_inputs) > 1) {
$change_data_input = true;
} elseif (cacti_sizeof($data_inputs) && $new_input_id != '' && $data_inputs[0]['data_input_id'] != $new_input_id) {
$change_data_input = true;
}
if ($change_data_input) {
if (cacti_sizeof($data_templates)) {
db_execute_prepared('UPDATE data_template_data
SET data_input_id = ?
WHERE data_input_id = ?
AND data_template_id IN (' . implode(', ', array_keys($data_templates)) . ')',
array($new_input_id, $previous_input_id));
push_out_data_input_method($new_input_id);
}
}
}
}
function get_data_query_array($snmp_query_id) {
global $config, $data_query_xml_arrays;
include_once($config['library_path'] . '/xml.php');
/* load the array into memory if it hasn't been done yet */
if (!isset($data_query_xml_arrays[$snmp_query_id])) {
$xml_file_path = db_fetch_cell_prepared('SELECT xml_path FROM snmp_query WHERE id = ?', array($snmp_query_id));
$search = array('<path_cacti>', '<path_snmpget>', '<path_php_binary>');
$replace = array($config['base_path'], read_config_option('path_snmpget'), read_config_option('path_php_binary'));
$xml_file_path = str_replace($search, $replace, $xml_file_path);
if (!file_exists($xml_file_path)) {
query_debug_timer_offset('data_query', __esc('Could not find data query XML file at \'%s\'', $xml_file_path));
return array();
}
query_debug_timer_offset('data_query', __esc('Found data query XML file at \'%s\'', $xml_file_path));
$data = implode('',file($xml_file_path));
$xml_data = xml2array($data);
/* snmp queries doe not require index_order
* get it from the fields if not set
*/
calculate_or_set_index_order($raw_data);
/* store the array value to the global array for future reference */
$data_query_xml_arrays[$snmp_query_id] = $xml_data;
}
return $data_query_xml_arrays[$snmp_query_id];
}
function query_script_host($host_id, $snmp_query_id) {
$script_queries = get_data_query_array($snmp_query_id);
/* invalid xml check */
if ((!is_array($script_queries)) || (cacti_sizeof($script_queries) == 0)) {
query_debug_timer_offset('data_query', __esc('Error parsing XML file into an array.'));
return false;
}
query_debug_timer_offset('data_query', __esc('XML file parsed ok.'));
/* are we talking to script server? */
if (isset($script_queries['script_server'])) {
$script_queries['script_path'] = "\"|path_php_binary|\" -q " . $script_queries['script_path'];
}
if (!verify_index_order($script_queries)) {
query_debug_timer_offset('data_query', __('Invalid field <index_order>%s</index_order>', $script_queries['index_order']));
query_debug_timer_offset('data_query', __('Must contain <direction>input</direction> or <direction>input-output</direction> fields only'));
return false;
}
// We get the current sort field. If for some reason the query
// fails for this field, or the snmp indexes we will give up.
$sort_field = get_best_data_query_index_type($host_id, $snmp_query_id);
/* provide data for arg_num_indexes, if given */
if (isset($script_queries['arg_num_indexes'])) {
$script_path = get_script_query_path((isset($script_queries['arg_prepend']) ? $script_queries['arg_prepend'] . ' ': '') . $script_queries['arg_num_indexes'], $script_queries['script_path'], $host_id);
/* fetch specified index at specified OID */
$script_num_index_array = exec_into_array($script_path);
// if the number of indexes does not exist use emulation
if (!cacti_sizeof($script_num_index_array)) {
query_debug_timer_offset('data_query', __esc('Data Query returned no indexes.'));
query_debug_timer_offset('data_query', __('<arg_num_indexes> exists in XML file but no data returned., \'Index Count Changed\' not supported'));
}
query_debug_timer_offset('data_query', __esc('Executing script for num of indexes \'%s\'', $script_path));
foreach ($script_num_index_array as $element) {
query_debug_timer_offset('data_query', __esc('Found number of indexes: %s' , $element));
}
} else {
if (isset($script_queries['script_server'])) {
query_debug_timer_offset('data_query', __('<arg_num_indexes> missing in XML file, \'Index Count Changed\' not supported'));
} else {
query_debug_timer_offset('data_query', __('<arg_num_indexes> missing in XML file, \'Index Count Changed\' emulated by counting arg_index entries'));
}
}
/* provide data for index, mandatory */
$script_path = get_script_query_path((isset($script_queries['arg_prepend']) ? $script_queries['arg_prepend'] . ' ': '') . $script_queries['arg_index'], $script_queries['script_path'], $host_id);
/* fetch specified index */
$script_index_array = exec_into_array($script_path);
if (!cacti_sizeof($script_index_array)) {
query_debug_timer_offset('data_query', __esc('ERROR: Data Query returned no indexes.'));
return false;
}
query_debug_timer_offset('data_query', __esc('Executing script for list of indexes \'%s\', Index Count: %s', $script_path, cacti_sizeof($script_index_array)));
debug_log_insert_section_start('data_query', __esc('Click to show Data Query output for \'index\''), true);
foreach ($script_index_array as $element) {
debug_log_insert('data_query', __esc('Found index: %s', $element));
}
debug_log_insert_section_end('data_query');
$empty_types = array();
/* set an array to host all updates */
$output_array = array();
foreach ($script_queries['fields'] as $field_name => $field_array) {
if ($field_array['direction'] == 'input' || $field_array['direction'] == 'input-output') {
$rewrite_value = isset($field_array['rewrite_value']) ? $field_array['rewrite_value'] : null;
$script_path = get_script_query_path((isset($script_queries['arg_prepend']) ? $script_queries['arg_prepend'] . ' ': '') . $script_queries['arg_query'] . ' ' . $field_array['query_name'], $script_queries['script_path'], $host_id);
debug_log_insert_section_start('data_query', __esc('Click to show Data Query output for field \'%s\'', $field_name), true);
$script_data_array = exec_into_array($script_path);
if (!cacti_sizeof($script_data_array) && $field_name == $sort_field) {
$empty_types[] = $field_name;
query_debug_timer_offset('data_query', __esc('Sort field returned no data for field name %s, skipping', $field_name));
} else {
debug_log_insert('data_query', __esc('Executing script query \'%s\'', $script_path));
if (cacti_sizeof($script_data_array)) {
foreach ($script_data_array as $element) {
// We have two spellings for 'delimiter' so keep both for backward compatibility
if (isset($script_queries['output_delimeter']) && preg_match("/(.*?)" . preg_quote($script_queries['output_delimeter']) . "(.*)/", $element, $matches)) {
$script_index = $matches[1];
$field_value = $matches[2];
$output_array[] = data_query_format_record($host_id, $snmp_query_id, $field_name, $rewrite_value, $field_value, $script_index, '');
debug_log_insert('data_query', __esc('Found item [%s=\'%s\'] index: %s', $field_name, $field_value, $script_index));
} elseif (isset($script_queries['output_delimiter']) && preg_match("/(.*?)" . preg_quote($script_queries['output_delimiter']) . "(.*)/", $element, $matches)) {
$script_index = $matches[1];
$field_value = $matches[2];
$output_array[] = data_query_format_record($host_id, $snmp_query_id, $field_name, $rewrite_value, $field_value, $script_index, '');
debug_log_insert('data_query', __esc('Found item [%s=\'%s\'] index: %s', $field_name, $field_value, $script_index));
}
}
}
}
debug_log_insert_section_end('data_query');
}
}
if (cacti_sizeof($output_array)) {
data_query_update_host_cache_from_buffer($host_id, $snmp_query_id, $output_array, $empty_types);
}
return true;
}
function query_debug_timer_start() {
global $query_debug_timer, $query_debug_start;
/* record the start time */
$query_debug_timer = microtime(true);
$query_debug_start = $query_debug_timer;
}
function query_debug_timer_offset($section, $message) {
global $query_debug_timer, $query_debug_start;
if (empty($query_debug_timer)) {
query_debug_timer_start();
}
/* record the start time */
$cur_time = microtime(true);
$delta = $cur_time - $query_debug_timer;
$total = $cur_time - $query_debug_start;
$query_debug_timer = $cur_time;
debug_log_insert($section, __('Total: %f, Delta: %f, %s', round($total, 2), round($delta, 2), $message));
return $delta;
}
function query_debug_timer_stop($section, $message) {
global $query_debug_timer, $query_debug_start;
/* record the start time */
$cur_time = microtime(true);
$delta = $cur_time - $query_debug_timer;
$total = $cur_time - $query_debug_start;
unset($query_debug_timer);
unset($query_debug_start);
debug_log_insert($section, __('Total: %f, Delta: %f, %s', round($total, 2), round($delta, 2), $message));
return $delta;
}
function query_snmp_host($host_id, $snmp_query_id) {
global $config, $data_query_rewrite_indexes_cache;
include_once($config['library_path'] . '/snmp.php');
$host = db_fetch_row_prepared('SELECT hostname, snmp_community, snmp_version,
snmp_username, snmp_password, snmp_auth_protocol, snmp_priv_passphrase,
snmp_priv_protocol, snmp_context, snmp_engine_id, snmp_port, snmp_timeout,
ping_retries, max_oids, bulk_walk_size
FROM host
WHERE id = ?',
array($host_id));
$snmp_queries = get_data_query_array($snmp_query_id);
// We get the current sort field. If for some reason the query
// fails for this field, or the snmp indexes we will give up.
$sort_field = get_best_data_query_index_type($host_id, $snmp_query_id);
if (!cacti_sizeof($host) || $host['hostname'] == '') {
query_debug_timer_offset('data_query', __esc('Invalid host_id: %s', $host_id));
return false;
}
/* invalid xml check */
if ((!is_array($snmp_queries)) || (cacti_sizeof($snmp_queries) == 0)) {
query_debug_timer_offset('data_query', __esc('Error parsing XML file into an array.'));
return false;
}
query_debug_timer_offset('data_query', __('XML file parsed ok.'));
if (!verify_index_order($snmp_queries)) {
query_debug_timer_offset('data_query', __('Invalid field <index_order>%s</index_order>', $snmp_queries['index_order']));
query_debug_timer_offset('data_query', __('Must contain <direction>input</direction> or <direction>input-output</direction> fields only'));
return false;
}
if ($host['bulk_walk_size'] <= 0) {
$walk_size = 5;
$walk_sizes = array(1, 5, 10, 15, 20, 25, 30, 40, 50, 60);
$low_total = 999;
$selected = -1;
query_debug_timer_offset('data_query', __esc('Auto Bulk Walk Size Selected.'));
foreach($walk_sizes as $size) {
$session = cacti_snmp_session($host['hostname'], $host['snmp_community'],
$host['snmp_version'], $host['snmp_username'], $host['snmp_password'],
$host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'],
$host['snmp_context'], $host['snmp_engine_id'], $host['snmp_port'],
$host['snmp_timeout'], $host['ping_retries'], $host['max_oids'], $size);
if ($session === false) {
debug_log_insert('data_query', __esc('Failed to load SNMP session.'));
return false;
}
/* fetch specified index at specified OID, measuring time */
$start = microtime(true);
$snmp_indexes = cacti_snmp_session_walk($session, $snmp_queries['oid_index']);
$end = microtime(true);
query_debug_timer_offset('data_query', __esc('Tested Bulk Walk Size %d with a response of %2.4f.', $size, $end - $start));
$total = $end - $start;
if ($total > $low_total) {
break;
} else {
$walk_size = $size;
$low_total = $total;
}
}
query_debug_timer_offset('data_query', __esc('Bulk Walk Size selected was %d.', $walk_size));
if ($host['bulk_walk_size'] == 0) {
query_debug_timer_offset('data_query', __esc('Saving Bulk Walk Size to Device.'));
$host['bulk_walk_size'] = $walk_size;
db_execute_prepared('UPDATE host
SET bulk_walk_size = ?
WHERE id = ?',
array($walk_size, $host_id));
}
} else {
$walk_size = $host['bulk_walk_size'];
query_debug_timer_offset('data_query', __esc('Bulk Walk Size is fixed at %d.', $walk_size));
$session = cacti_snmp_session($host['hostname'], $host['snmp_community'],
$host['snmp_version'], $host['snmp_username'], $host['snmp_password'],
$host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'],
$host['snmp_context'], $host['snmp_engine_id'], $host['snmp_port'],
$host['snmp_timeout'], $host['ping_retries'], $host['max_oids'], $walk_size);
if ($session === false) {
debug_log_insert('data_query', __esc('Failed to load SNMP session.'));
return false;
}
/* fetch specified index at specified OID */
$snmp_indexes = cacti_snmp_session_walk($session, $snmp_queries['oid_index']);
}
/* provide data for oid_num_indexes, if given */
if (isset($snmp_queries['oid_num_indexes'])) {
$snmp_num_indexes = cacti_snmp_session_get($session, $snmp_queries['oid_num_indexes']);
query_debug_timer_offset('data_query', __esc('Executing SNMP get for num of indexes @ \'%s\' Index Count: %s' , $snmp_queries['oid_num_indexes'] , $snmp_num_indexes));
} else {
query_debug_timer_offset('data_query', __('<oid_num_indexes> missing in XML file, \'Index Count Changed\' emulated by counting oid_index entries'));
}
query_debug_timer_offset('data_query', __esc('Executing SNMP walk for list of indexes @ \'%s\' Index Count: %s', $snmp_queries['oid_index'] , cacti_sizeof($snmp_indexes)));
/* no data found; get out */
if (!cacti_sizeof($snmp_indexes)) {
query_debug_timer_offset('data_query', __esc('No SNMP data returned'));
return false;
} else {
/* show list of indices found */
foreach ($snmp_indexes as $oid => $value) {
query_debug_timer_offset('data_query', __esc('Index found at OID: \'%s\' value: \'%s\'', $oid , $value));
}
}
/* the last octet of the oid is the index by default */
$index_parse_regexp = '/.*\.([0-9]+)$/';
/* Filtered index by value */
if (isset($snmp_queries['value_index_parse'])) {
$value_parse_regexp = '/' . str_replace('VALUE/REGEXP:', '', $snmp_queries['value_index_parse']) . '/';
foreach ($snmp_indexes as $oid => $value) {
if (!preg_match($value_parse_regexp, $value)) {
unset($snmp_indexes[$oid]);
}
}
query_debug_timer_offset('data_query', __esc('List of indexes filtered by value @ \'%s\' Index Count: %s', $snmp_queries['oid_index'] , cacti_sizeof($snmp_indexes)));
/* show list of indices found */
foreach ($snmp_indexes as $oid => $value) {
query_debug_timer_offset('data_query', __esc('Filtered Index by value found at OID: \'%s\' value: \'%s\'', $oid , $value));
}
}
/* parse the index if required */
if (isset($snmp_queries['oid_index_parse'])) {
$index_parse_regexp = '/' . str_replace('OID/REGEXP:', '', $snmp_queries['oid_index_parse']) . '/';
$parsed_indexes = array();
foreach ($snmp_indexes as $oid => $value) {
if (preg_match($index_parse_regexp, $oid)) {
$parsed_indexes[$oid] = preg_replace($index_parse_regexp, "\\1", $oid);
}
}
$snmp_indexes = $parsed_indexes;
query_debug_timer_offset('data_query', __esc('Filtering list of indexes @ \'%s\' Index Count: %s', $snmp_queries['oid_index'] , cacti_sizeof($snmp_indexes)));
/* show list of indices found */
foreach ($snmp_indexes as $oid => $value) {
query_debug_timer_offset('data_query', __esc('Filtered Index found at OID: \'%s\' value: \'%s\'', $oid , $value));
}
}
/* set an array to host all updates */
$output_array = array();
/* invalidate rewrite_index cache */
$data_query_rewrite_indexes_cache = array();
$fields_processed = array();
$empty_types = array();
rewrite_snmp_enum_value(null);
foreach ($snmp_queries['fields'] as $field_name => $field_array) {
if ($field_array['source'] != 'index' && ($field_array['direction'] == 'input' || $field_array['direction'] == 'input-output') && $field_array['method'] != 'get' &&
(isset($field_array['rewrite_index']) || isset($field_array['oid_suffix']))) {
$field_array['method'] = 'get';
debug_log_insert('data_query', __esc('Fixing wrong \'method\' field for \'%s\' since \'rewrite_index\' or \'oid_suffix\' is defined',$field_name));
}
$rewrite_value = isset($field_array['rewrite_value']) ? $field_array['rewrite_value'] : null;
if ((!isset($field_array['oid'])) && ($field_array['source'] == 'index')) {
foreach ($snmp_indexes as $oid => $value) {
query_debug_timer_offset('data_query', __esc('Inserting index data for field \'%s\' [value=\'%s\']' , $field_name, $value));
$output_array[] = data_query_format_record($host_id, $snmp_query_id, $field_name, $rewrite_value, $value, $value, '');
}
} elseif (($field_array['method'] == 'get') && ($field_array['direction'] == 'input' || $field_array['direction'] == 'input-output')) {
query_debug_timer_offset('data_query', __esc('Located input field \'%s\' [get]',$field_name));
if ($field_array['source'] == 'value' && !isset($field_array['rewrite_index'])) {
$oid_rewrite_pattern = null;
$oid_rewrite_replacement = null;
if (isset($field_array['oid_rewrite_pattern']) && isset($field_array['oid_rewrite_replacement'])) {
$oid_rewrite_pattern = '/' . str_replace('OID/REGEXP:', '', $field_array['oid_rewrite_pattern']) . '/';
$oid_rewrite_replacement = $field_array['oid_rewrite_replacement'];
query_debug_timer_offset('data_query', __esc('Found OID rewrite rule: \'s/%s/%s/\'', $oid_rewrite_pattern,$oid_rewrite_replacement));
}
foreach ($snmp_indexes as $oid => $index) {
$oid = $field_array['oid'] . '.' . $index;
$oid .= isset($field_array['oid_suffix']) ? ('.' . $field_array['oid_suffix']) : '';
/* rewrite the oid if required */
if (isset($oid_rewrite_pattern)) {
$orig_oid = $oid;
$oid = preg_replace($oid_rewrite_pattern, $oid_rewrite_replacement, $oid);
query_debug_timer_offset('data_query', __esc('oid_rewrite at OID: \'%s\' new OID: \'%s\'', $orig_oid , $oid));
}
if (isset($field_array['output_format'])) {
if ($field_array['output_format'] == 'hex') {
$value_output_format = SNMP_STRING_OUTPUT_HEX;
} elseif ($field_array['output_format'] == 'ascii') {
$value_output_format = SNMP_STRING_OUTPUT_ASCII;
} else {
$value_output_format = SNMP_STRING_OUTPUT_GUESS;
}
$value = cacti_snmp_get($host['hostname'], $host['snmp_community'], $oid, $host['snmp_version'],
$host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'],
$host['snmp_priv_passphrase'], $host['snmp_priv_protocol'], $host['snmp_context'],
$host['snmp_port'], $host['snmp_timeout'], SNMP_POLLER, $host['snmp_engine_id'],
$value_output_format);
} else {
$value = cacti_snmp_session_get($session, $oid);
}
query_debug_timer_offset('data_query', __esc('Executing SNMP get for data @ \'%s\' [value=\'%s\']', $oid, $value));
$output_array[] = data_query_format_record($host_id, $snmp_query_id, $field_name, $rewrite_value, $value, $index, $oid);
}
} elseif (isset($field_array['rewrite_index'])) {
$rewritten_indexes = array();
if (isset($field_array['rewrite_index'])) {
$rewritten_indexes = data_query_rewrite_indexes($errmsg, $host_id, $snmp_query_id, $field_array['rewrite_index'], $snmp_indexes, $fields_processed);
if (cacti_sizeof($errmsg)) {
foreach ($errmsg as $message) {
debug_log_insert('data_query', __esc('Field \'%s\' %s', $field_name,$message));
}
}
}
$values = array();
foreach ($snmp_indexes as $index_oid => $index) {
$oid = $field_array['oid'];
if (isset($field_array['rewrite_index'])) {
if (isset($rewritten_indexes[$index])) {
$oid_suffix = $rewritten_indexes[$index];
} else {
// we failed to build rewritten index. warnings are sent already, just skip this index silently
continue;
}
$oid .= '.' . $oid_suffix;
} else {
$oid .= '.' . $index;
}
$oid .= isset($field_array['oid_suffix']) ? ('.' . $field_array['oid_suffix']) : '';
$value = null;
if (substr($field_array['source'], 0, 11) == 'OID/REGEXP:') {
$value = preg_replace('/' . str_replace('OID/REGEXP:', '', $field_array['source']) . '/', "\\1", $oid);
} elseif (substr($field_array['source'], 0, 15) == 'OID2HEX/REGEXP:') {
$value = preg_replace('/' . str_replace('OID2HEX/REGEXP:', '', $field_array['source']) . '/', "\\1", $oid);
$parts = explode('.', $value);
$ip_value = '';
foreach($parts as $idx => $part) {
if (is_numeric($part)) {
$parts[$idx] = substr(strtoupper('00' . dechex($part)), -2);
} else {
$parts[$idx] = substr(strtoupper('00' . $part), -2);
}
if ($idx % 2 == 0 && $idx > 0) {
$ip_value .= ':' . $parts[$idx];
} else {
$ip_value .= $parts[$idx];
}
}
$value = implode(':', $parts);
// Check for ip address and shorten
if (is_ipaddress($ip_value)) {
$value = inet_ntop(inet_pton($ip_value));
}
}
$values[] = array('value' => $value, 'index' => $index, 'oid' => $oid);
}
if (cacti_sizeof($values) > 0 && (substr($field_array['source'], 0, 13) == 'VALUE/REGEXP:' || $field_array['source'] == 'value')) {
$oids = array();
foreach ($values as $key => $value) {
$oids[] = $value['oid'];
}
debug_log_insert('data_query', __esc('Executing SNMP get for %s oids (%s)' , cacti_count($oids), implode(', ', $oids)));
$value_output_format = SNMP_STRING_OUTPUT_GUESS;
if (isset($field_array['output_format'])) {
if ($field_array['output_format'] == 'hex') {
$value_output_format = SNMP_STRING_OUTPUT_HEX;
} elseif ($field_array['output_format'] == 'ascii') {
$value_output_format = SNMP_STRING_OUTPUT_ASCII;
} else {
$value_output_format = SNMP_STRING_OUTPUT_GUESS;
}
}
if (!empty($value_output_format)) {
foreach ($oids as $oid) {
$results[$oid] = cacti_snmp_get($host['hostname'], $host['snmp_community'], $oid, $host['snmp_version'],
$host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'],
$host['snmp_priv_passphrase'], $host['snmp_priv_protocol'], $host['snmp_context'],
$host['snmp_port'], $host['snmp_timeout'], SNMP_POLLER, $host['snmp_engine_id'],
$value_output_format);
}
} else {
$results = cacti_snmp_session_walk($session, $oids);
}
if (!cacti_sizeof($results) && $field_name == $sort_field) {
query_debug_timer_offset('data_query', __esc('Sort field returned no data for OID[%s], skipping.', $oid));
} elseif (cacti_sizeof($results)) {
foreach ($results as $key => $value) {
debug_log_insert('data_query',
__esc('Found result for data @ \'%s\' [value=\'%s\']',
$key, $value));
}
foreach (array_keys($values) as $key) {
if (isset($results[$values[$key]['oid']])) {
$values[$key]['value'] = $results[$values[$key]['oid']];
debug_log_insert('data_query',
__esc('Setting result for data @ \'%s\' [key=\'%s\', value=\'%s\']',
$values[$key]['oid'], $key, $values[$key]['value']));
} else {
debug_log_insert('data_query',
__esc('Skipped result for data @ \'%s\' [key=\'%s\', value=\'%s\']',
$values[$key]['oid'], $key, $values[$key]['value']));
}
}
foreach ($values as $key => $value) {
if (substr($field_array['source'], 0, 13) == 'VALUE/REGEXP:') {
$values[$key]['value'] = preg_replace('/' . str_replace('VALUE/REGEXP:', '', $field_array['source']) . '/', "\\1", $values[$key]['value']);
}
}
}
}
foreach ($values as $item) {
debug_log_insert('data_query', __esc('Got SNMP get result for data @ \'%s\' [value=\'%s\'] (index: %s)', $item['oid'], $item['value'], $item['index']));
$output_array[] = data_query_format_record($host_id, $snmp_query_id, $field_name, $rewrite_value, $item['value'], $item['index'], $item['oid']);
}
$values = null;
} elseif (substr($field_array['source'], 0, 13) == 'VALUE/REGEXP:') {
foreach ($snmp_indexes as $oid => $index) {
$oid = $field_array['oid'] . '.' . $index;
$oid .= isset($field_array['oid_suffix']) ? ('.' . $field_array['oid_suffix']) : '';
if (isset($field_array['output_format'])) {
if ($field_array['output_format'] == 'hex') {
$value_output_format = SNMP_STRING_OUTPUT_HEX;
} elseif ($field_array['output_format'] == 'ascii') {
$value_output_format = SNMP_STRING_OUTPUT_ASCII;
} else {
$value_output_format = SNMP_STRING_OUTPUT_GUESS;
}
$value = cacti_snmp_get($host['hostname'], $host['snmp_community'], $oid, $host['snmp_version'],
$host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'],
$host['snmp_priv_passphrase'], $host['snmp_priv_protocol'], $host['snmp_context'],
$host['snmp_port'], $host['snmp_timeout'], SNMP_POLLER, $host['snmp_engine_id'],
$value_output_format);
} else {
$value = cacti_snmp_session_get($session, $oid);
}
$value = preg_replace('/' . str_replace('VALUE/REGEXP:', '', $field_array['source']) . '/', "\\1", $value);
query_debug_timer_offset('data_query', __esc('Executing SNMP get for data @ \'%s\' [value=\'$value\']', $oid, $value));
$output_array[] = data_query_format_record($host_id, $snmp_query_id, $field_name, $rewrite_value, $value, $index, $oid);
}
}
} elseif ($field_array['method'] == 'walk' && ($field_array['direction'] == 'input' || $field_array['direction'] == 'input-output')) {
debug_log_insert_section_start('data_query', __esc('Click to show Data Query output for field \'%s\'', $field_name), true);
query_debug_timer_offset('data_query', __esc('Located input field \'%s\' [walk]', $field_name));
if (isset($field_array['output_format'])) {
if ($field_array['output_format'] == 'hex') {
$value_output_format = SNMP_STRING_OUTPUT_HEX;
} elseif ($field_array['output_format'] == 'ascii') {
$value_output_format = SNMP_STRING_OUTPUT_ASCII;
} else {
$value_output_format = SNMP_STRING_OUTPUT_GUESS;
}
$data = cacti_snmp_walk($host['hostname'], $host['snmp_community'], $field_array['oid'], $host['snmp_version'],
$host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'],
$host['snmp_priv_passphrase'], $host['snmp_priv_protocol'], $host['snmp_context'],
$host['snmp_port'], $host['snmp_timeout'], $host['ping_retries'], $host['max_oids'], SNMP_POLLER,
$host['snmp_engine_id'], $value_output_format);
$snmp_data = array();
if (cacti_sizeof($data)) {
foreach ($data as $d) {
$snmp_data[$d['oid']] = $d['value'];
}
} elseif ($field_name == $sort_field) {
$empty_types[] = $field_name;
query_debug_timer_offset('data_query', __esc('Sort field returned no data for OID[%s], skipping.', $field_array['oid']));
continue;
}
} else {
$snmp_data = cacti_snmp_session_walk($session, $field_array['oid']);
if (!cacti_sizeof($snmp_data) && $field_name == $sort_field) {
$empty_types[] = $field_name;
query_debug_timer_offset('data_query', __esc('Sort field returned no data for OID[%s], skipping.', $field_array['oid']));
continue;
}
}
query_debug_timer_offset('data_query', __esc('Executing SNMP walk for data @ \'%s\'', $field_array['oid']));
if (preg_match('/^VALUE\/TABLE:(.*)/',$field_array['source'],$matches)) {
preg_match_all('/([^:]+):([^:]+)/',$matches[1],$match_temp);
$const_table = array_combine($match_temp[1],$match_temp[2]);
}
if (preg_match('/^value/i', $field_array['source'])) {
if (cacti_sizeof($snmp_data)) {
foreach ($snmp_data as $oid => $value) {
$index_regex = (isset($field_array['oid_index_parse']) ? $field_array['oid_index_parse'] : $index_parse_regexp) . (isset($field_array['oid_suffix']) ? ('.' . $field_array['oid_suffix']) : '');
if (!preg_match($index_regex, $oid)) {
continue;
}
$snmp_index = preg_replace($index_regex,"\\1", $oid);
if (isset($snmp_queries['value_index_parse'])) {
if (!in_array($snmp_index, $snmp_indexes)) {
debug_log_insert('data_query', __esc('No index[%s] in value_index_parse, skipping.', $snmp_index));
unset($snmp_data[$oid]);
continue;
}
}
$oid = $field_array['oid'] . ".$snmp_index" . (isset($field_array['oid_suffix']) ? ('.' . $field_array['oid_suffix']) : '');
if ($field_name == 'ifOperStatus' || $field_name == 'ifAdminStatus') {
switch(true) {
case preg_match('/^(down|2)/i',$value):
$value = 'Down';
break;
case preg_match('/^(up|1)/i',$value):
$value = 'Up';
break;
case preg_match('/^(notpresent|6)/i',$value):
$value = 'notPresent';
break;
default:
$value = 'Testing';
}
$mode = 'value';
} elseif (preg_match('/VALUE\/REGEXP:(.*)/', $field_array['source'], $matches)) {
$modified_value = preg_replace('/' . $matches[1] . '/', "\\1", $value);
$mode = 'regexp value parse';
} elseif (preg_match('/^VALUE\/TEST:(.*):(.*):(.*)/', $field_array['source'], $matches)) {
$modified_value = (strcmp($matches[1], $value) !== 0) ? $matches[3]:$matches[2];
$mode = 'test value parse';
} elseif (preg_match('/^VALUE\/TABLE:(.*)/',$field_array['source'])) {
$modified_value = (array_key_exists($value, $const_table)) ? $const_table[$value]:'N/A';
$mode = 'table value parse';
} elseif (preg_match('/^VALUE\/HEX2IP:(\d+):(\d+)/', $field_array['source'], $matches)) {
#$modified_value = substr(str_replace(':','',$value),$matches[1],$matches[2]);
$modified_value = substr(preg_replace('/(:|HEX-)/','', $value), $matches[1], $matches[2]);
$modified_value = implode('.', unpack ('C*', pack('H*', $modified_value)));
$mode = 'hex2ip value parse';
} else {
$mode = 'value';
}
$output_array[] = data_query_format_record($host_id, $snmp_query_id, $field_name, $rewrite_value, isset($modified_value)?$modified_value:$value , $snmp_index, $oid);
debug_log_insert('data_query', __esc('Found item [%s=\'%s\'] index: %s [from %s]', $field_name, isset($modified_value) ? "$modified_value ($value)":$value, $snmp_index, $mode));
unset($modified_value);
}
}
} elseif (substr($field_array['source'], 0, 11) == 'OID/REGEXP:') {
if (cacti_sizeof($snmp_data)) {
foreach ($snmp_data as $oid => $value) {
$parse_value = preg_replace('/' . str_replace('OID/REGEXP:', '', $field_array['source']) . '/', "\\1", $oid);
if (isset($snmp_queries['oid_index_parse'])) {
$snmp_index = preg_replace($index_parse_regexp, "\\1", $oid);
} elseif ((isset($value)) && ($value != '')) {
$snmp_index = $value;
}
if (isset($snmp_queries['value_index_parse'])) {
if (!in_array($snmp_index, $snmp_indexes)) {
debug_log_insert('data_query', __esc('No index[%s] in value_index_parse, skipping.', $snmp_index));
unset($snmp_data[$oid]);
continue;
}
}
$oid = $field_array['oid'] . '.' . $parse_value;
/* rewrite octet strings */
if (preg_match('/^\d{1,3}(\.\d{1,3}){2,}$/', $parse_value)) {
$octets = explode('.', $parse_value);
$size = array_shift($octets);
if (cacti_count($octets) == $size) {
$decoded = '';
$isascii = true;
foreach ($octets as $octet) {
if (($octet <= 31) || ($octet >= 127)) {
$isascii = false;
} else {
$decoded .= chr($octet);
}
}
if ($isascii) {
query_debug_timer_offset('data_query', __esc('Found OCTET STRING \'%s\' decoded value: \'%s\'', $parse_value, $decoded));
$parse_value = $decoded;
}
}
}
debug_log_insert('data_query', __esc('Found item [%s=\'%s\'] index: %s [from regexp oid parse]', $field_name, $parse_value, $snmp_index));
$output_array[] = data_query_format_record($host_id, $snmp_query_id, $field_name, $rewrite_value, $parse_value, $snmp_index, $oid);
}
}
} elseif (substr($field_array['source'], 0, 15) == 'OID2HEX/REGEXP:') {
if (cacti_sizeof($snmp_data)) {
foreach ($snmp_data as $oid => $value) {
$parse_value = preg_replace('/' . str_replace('OID2HEX/REGEXP:', '', $field_array['source']) . '/', "\\1", $oid);
if (isset($snmp_queries['oid_index_parse'])) {
$snmp_index = preg_replace($index_parse_regexp, "\\1", $oid);
} elseif ((isset($value)) && ($value != '')) {
$snmp_index = $value;
}
if (isset($snmp_queries['value_index_parse'])) {
if (!in_array($snmp_index, $snmp_indexes)) {
debug_log_insert('data_query', __esc('No index[%s] in value_index_parse, skipping.', $snmp_index));
unset($snmp_data[$oid]);
continue;
}
}
$oid = $field_array['oid'] . '.' . $parse_value;
$ip_value = '';
// Check for an IPv6 Address or Hex String
$parts = explode('.', $parse_value);
foreach($parts as $idx => $part) {
if (is_numeric($part)) {
$parts[$idx] = substr(strtoupper('00' . dechex($part)), -2);
} else {
$parts[$idx] = substr(strtoupper('00' . $part), -2);
}
if ($idx % 2 == 0 && $idx > 0) {
$ip_value .= ':' . $parts[$idx];
} else {
$ip_value .= $parts[$idx];
}
}
$parse_value = implode(':', $parts);
// Check for ip address and shorten
if (is_ipaddress($ip_value)) {
$parse_value = inet_ntop(inet_pton($ip_value));
}
debug_log_insert('data_query', __esc('Found item [%s=\'%s\'] index: %s [from regexp oid parse]', $field_name, $parse_value, $snmp_index));
$output_array[] = data_query_format_record($host_id, $snmp_query_id, $field_name, $rewrite_value, $parse_value, $snmp_index, $oid);
}
}
} elseif (substr($field_array['source'], 0, 16) == 'OIDVALUE/REGEXP:') {
$regex_array = explode(':', str_replace('OIDVALUE/REGEXP:', '', $field_array['source']));
if (cacti_sizeof($snmp_data)) {
foreach($snmp_data as $oid => $value) {
$parse_value = preg_replace('/' . $regex_array[0] . '/', $regex_array[1], $oid);
if (isset($snmp_queries['oid_index_parse'])) {
$snmp_index = preg_replace($index_parse_regexp, "\\1", $oid);
} elseif ((isset($value)) && ($value != '')) {
$snmp_index = $value;
}
if (isset($snmp_queries['value_index_parse'])) {
if (!in_array($snmp_index, $snmp_indexes)) {
debug_log_insert('data_query', __esc('No index[%s] in value_index_parse, skipping.', $snmp_index));
unset($snmp_data[$oid]);
continue;
}
}
$oid = $field_array['oid'];
debug_log_insert('data_query', __esc('Found item [%s=\'%s\'] index: %s [from regexp oid value parse]', $field_name, $parse_value, $snmp_index));
$output_array[] = data_query_format_record($host_id, $snmp_query_id, $field_name, $rewrite_value, $parse_value, $snmp_index, $oid);
}
}
}
debug_log_insert_section_end('data_query');
}
$fields_processed[] = $field_name;
}
$session->close();
if (cacti_sizeof($output_array)) {
data_query_update_host_cache_from_buffer($host_id, $snmp_query_id, $output_array, $empty_types);
}
return true;
}
function data_query_format_record($host_id, $snmp_query_id, $field_name, $rewrite_value, $value, $snmp_index, $oid) {
global $data_query_rewrite_indexes_cache;
if ($rewrite_value !== null) {
$value = rewrite_snmp_enum_value($field_name, $value, $rewrite_value);
}
$hash = "$host_id@$snmp_query_id@$field_name";
if (!isset($data_query_rewrite_indexes_cache[$hash])) {
$data_query_rewrite_indexes_cache[$hash] = array();
}
$data_query_rewrite_indexes_cache[$hash][$snmp_index] = $value;
if (mb_detect_encoding($value) === false) {
$value = bin2hex($value);
}
return "($host_id, $snmp_query_id, " . db_qstr($field_name) . ', ' . db_qstr($value) . ', ' . db_qstr($snmp_index) . ', ' . db_qstr($oid) . ', 1)';
}
function data_query_ctype_print_unicode($value) {
$pattern = "~^[\pL\pN\s\"\~" . preg_quote("!#$%&'()*+,-./:;<=>?@[\]^_`{|}´") . "]+$~u";
return preg_match($pattern, $value);
}
function data_query_update_host_cache_from_buffer($host_id, $snmp_query_id, &$output_array, &$empty_types) {
/* set all fields present value to 0, to mark the outliers when we are all done */
db_execute_prepared('UPDATE host_snmp_cache
SET present = 0
WHERE host_id = ?
AND snmp_query_id = ?',
array($host_id, $snmp_query_id));
/* setup the database call */
$sql_prefix = 'INSERT INTO host_snmp_cache (host_id, snmp_query_id, field_name, field_value, snmp_index, oid, present) VALUES';
$sql_suffix = ' ON DUPLICATE KEY UPDATE field_value=VALUES(field_value), oid=VALUES(oid), present=VALUES(present)';
/* use a reasonable insert buffer, the default is 1MByte */
$max_packet = 256000;
/* setup some defaults */
$overhead = strlen($sql_prefix) + strlen($sql_suffix);
$buf_len = 0;
$buf_count = 0;
$buffer = '';
foreach ($output_array as $record) {
if ($buf_count == 0) {
$delim = ' ';
} else {
$delim = ', ';
}
$buffer .= $delim . $record;
$buf_len += strlen($record);
if (($overhead + $buf_len) > ($max_packet - 1024)) {
db_execute($sql_prefix . $buffer . $sql_suffix);
$buffer = '';
$buf_len = 0;
$buf_count = 0;
} else {
$buf_count++;
}
}
if ($buf_count > 0) {
db_execute($sql_prefix . $buffer . $sql_suffix);
}
if (cacti_sizeof($empty_types)) {
foreach($empty_types as $field) {
db_execute_prepared('UPDATE host_snmp_cache
SET present = 1
WHERE host_id = ?
AND snmp_query_id = ?
AND field_name = ?',
array($host_id, $snmp_query_id, $field));
}
}
/* remove stale records from the host cache */
db_execute_prepared("DELETE FROM host_snmp_cache
WHERE host_id = ?
AND snmp_query_id = ?
AND present='0'",
array($host_id, $snmp_query_id));
}
/* data_query_rewrite_indexes - returns array of rewritten indexes
@arg $errmsg array that will contain warnings if any
@arg $host_id
@arg $snmp_query_id
@arg $rewrite_index - value of <rewrite_index> from data query XML
@arg $snmp_indexes - array of snmp indexes as it used in query_snmp_host() or single index
@arg $fields_processed - array of field names that are already processed in query_snmp_host(),
refusing non-processed (e.g. stale) fields to be used as index rewrite source
@returns - (array) of original snmp indexes associated with rewritten ones
*/
function data_query_rewrite_indexes(&$errmsg, $host_id, $snmp_query_id, $rewrite_index, $snmp_indexes, $fields_processed = false) {
global $data_query_rewrite_indexes_cache;
$errmsg = array();
$oid_items = explode('.', $rewrite_index);
$chain_indexes = array();
if (!is_array($data_query_rewrite_indexes_cache))
$data_query_rewrite_indexes_cache = array();
if (cacti_sizeof($oid_items)) {
foreach ($oid_items as $item) {
$matches = array();
if (preg_match('/^\|query_([^|]+)\|$/', $item, $matches)) {
$iv = $matches[1];
if (is_array($fields_processed) && !in_array($iv, $fields_processed)) {
$errmsg[] = "rewrite_index='$rewrite_index': '$iv' is not processed yet, could not use it as index source";
continue;
}
if (!isset($chain_indexes[$iv])) {
$hash = "$host_id@$snmp_query_id@$iv";
if (!isset($data_query_rewrite_indexes_cache[$hash])) {
$data_query_rewrite_indexes_cache[$hash] = array();
$iv = db_qstr($iv);
$field_values = db_fetch_assoc_prepared("SELECT snmp_index, field_value
FROM host_snmp_cache
WHERE host_id = ?
AND snmp_query_id = ?
AND field_name = ?",
array($host_id, $snmp_query_id, $iv));
if (cacti_sizeof($field_values)) {
foreach ($field_values as $item) {
$data_query_rewrite_indexes_cache[$hash][$item['snmp_index']] = $item['field_value'];
}
} else {
$errmsg[] = " field '$iv' was not found either in cache nor in DB";
}
}
$chain_indexes[$iv] = $data_query_rewrite_indexes_cache[$hash];
}
}
}
}
$out = array();
$numeric_output = false;
if (!is_array($snmp_indexes)) {
$snmp_indexes = array($snmp_indexes);
$numeric_output = true;
}
foreach ($snmp_indexes as $oid => $num_index) {
$index = $rewrite_index;
foreach ($chain_indexes as $key => $values) {
if (isset($values[$num_index]) && preg_match('/^[0-9.]+$/', $values[$num_index])) {
$index = str_replace("|query_$key|", trim($values[$num_index]), $index);
} else {
$errmsg[] = "@'" . $num_index . "': could not load value of '$key'";
}
}
$index = str_replace('|index|', trim($num_index), $index);
if (!preg_match('/^[0-9.]*$/', $index)) {
$errmsg[] = "@'" . $num_index ."': some portions of rewrite_index field were not parsed: '$index'";
continue;
}
if ($numeric_output) {
return $index;
}
$out[$num_index] = $index;
}
if ($numeric_output) {
return null;
}
return $out;
}
/* rewrite_snmp_enum_value - returns rewritten $value based on rewrite map
@arg $field_name - name of field being rewritten, used for cache purposes
@arg $value - value to be translated
@arg $map - translation map in serialize()/array form
@returns - rewritten value if possible, original one otherwise*/
function rewrite_snmp_enum_value($field_name, $value = null, $map = null) {
static $mapcache = array();
if ($field_name === null) {
$mapcache = array();
return null;
}
if (is_array($map)) { # produced from XML, needs to be reformatted
$newmap = array();
foreach ($map as $index => $item) {
if (!isset($item['match']) || !isset($item['replace'])) {
debug_log_insert('data_query', __esc('Bogus rewrite_value item found, index=\'%s\'', $index));
continue;
}
$newmap[$item['match']] = $item['replace'];
}
$map = $newmap;
} else {
$map = cacti_unserialize($map);
}
if ($map === false || !is_array($map)) {
debug_log_insert('data_query', __esc('Could not parse translation map (rewrite_value)'));
return $value;
}
if (!isset($mapcache[$field_name])) {
$mapcache[$field_name] = array();
foreach ($map as $src => $dst) {
if (preg_match('/^REGEXP(NC)?:(.*)$/', $src, $matches)) {
if ($matches[1] == 'NC') {
$src = '/' . str_replace('/', '\/', $matches[2]) . '/i';
} else {
$src = '/' . str_replace('/', '\/', $matches[1]) . '/';
}
} else {
$src = '/^' . str_replace('/^', '\/', $src) . '$/';
}
$mapcache[$field_name][$src] = $dst;
}
}
foreach ($mapcache[$field_name] as $src => $dst) {
if (preg_match($src, $value)) {
$nvalue = preg_replace($src, $dst, $value);
debug_log_insert('data_query', __esc('rewrite_value: \'%s\' => \'%s\'', $value, $nvalue));
$value = $nvalue;
break;
}
}
return $value;
}
/* data_query_index - returns an array containing the data query ID and index value given
a data query index type/value combination and a host ID
@arg $index_type - the name of the index to match
@arg $index_value - the value of the index to match
@arg $host_id - (int) the host ID to match
@arg $data_query_id - (int) the data query ID to match
@returns - (array) the data query ID and index that matches the three arguments */
function data_query_index($index_type, $index_value, $host_id, $data_query_id) {
return db_fetch_cell_prepared("SELECT snmp_index
FROM host_snmp_cache
WHERE field_name = ?
AND field_value = ?
AND host_id = ?
AND snmp_query_id = ?",
array($index_type, $index_value, $host_id, $data_query_id));
}
/* data_query_field_list - returns an array containing data query information for a given data source
@arg $data_template_data_id - the ID of the data source to retrieve information for
@returns - (array) an array that looks like:
Array
(
[index_type] => ifIndex
[index_value] => 3
[output_type] => 13
) */
function data_query_field_list($data_template_data_id) {
if (!is_numeric($data_template_data_id)) {
return 0;
}
$field = db_fetch_assoc_prepared("SELECT dif.type_code, did.value
FROM data_input_fields AS dif
INNER JOIN data_input_data AS did
ON dif.id=did.data_input_field_id
WHERE did.data_template_data_id = ?
AND dif.type_code IN ('index_type','index_value','output_type')", array($data_template_data_id));
$field = array_rekey($field, 'type_code', 'value');
if ((!isset($field['index_type'])) || (!isset($field['index_value'])) || (!isset($field['output_type']))) {
return 0;
} else {
return $field;
}
}
/* encode_data_query_index - encodes a data query index value so that it can be included
inside of a form
@arg $index - the index name to encode
@returns - the encoded data query index */
function encode_data_query_index($index) {
return md5($index);
}
/* decode_data_query_index - decodes a data query index value so that it can be read from
a form
@arg $encoded_index - the index that was encoded with encode_data_query_index()
@arg $data_query_id - the id of the data query that this index belongs to
@arg $encoded_index - the id of the host that this index belongs to
@returns - the decoded data query index */
function decode_data_query_index($encoded_index, $data_query_id, $host_id) {
/* yes, i know MySQL has a MD5() function that would make this a bit quicker. however i would like to
keep things abstracted for now so Cacti works with ADODB fully when i get around to porting my db calls */
$indexes = db_fetch_assoc_prepared('SELECT snmp_index
FROM host_snmp_cache
WHERE host_id = ?
AND snmp_query_id = ?
GROUP BY snmp_index', array($host_id, $data_query_id));
if (cacti_sizeof($indexes)) {
foreach ($indexes as $index) {
if (encode_data_query_index($index['snmp_index']) == $encoded_index) {
return $index['snmp_index'];
}
}
}
}
/* update_data_query_cache - updates the local data query cache for each graph AND data
source tied to this host/data query
@arg $host_id - the id of the host to refresh
@arg $data_query_id - the id of the data query to refresh */
function update_data_query_cache($host_id, $data_query_id) {
$graphs = db_fetch_assoc_prepared('SELECT *
FROM graph_local
WHERE host_id = ?
AND snmp_query_id = ?',
array($host_id, $data_query_id));
if (cacti_sizeof($graphs)) {
foreach ($graphs as $graph) {
update_graph_data_query_cache($graph['id'], $host_id, $data_query_id, $graph['snmp_index']);
}
}
query_debug_timer_offset('data_query', __esc('Update graph data query cache complete'));
$data_sources = db_fetch_assoc_prepared('SELECT *
FROM data_local
WHERE host_id = ?
AND snmp_query_id = ?',
array($host_id, $data_query_id));
if (cacti_sizeof($data_sources) > 0) {
foreach ($data_sources as $data_source) {
$changed = update_data_source_data_query_cache($data_source['id'], $host_id, $data_query_id, $data_source['snmp_index']);
if ($changed) {
update_poller_cache($data_source, true);
}
}
}
query_debug_timer_offset('data_query', __esc('Re-Indexing Data Query complete'));
}
/* update_graph_data_query_cache - updates the local data query cache for a particular
graph
@arg $local_graph_id - the id of the graph to update the data query cache for */
function update_graph_data_query_cache($local_graph_id, $host_id = '', $data_query_id = '', $previous_index = '') {
global $data_query_id_cache;
if ((!is_array($data_query_id_cache)) || cacti_sizeof($data_query_id_cache) == 0) {
$data_query_id_cache = array_rekey(
db_fetch_assoc('SELECT id, snmp_query_id
FROM snmp_query_graph'),
'id', 'snmp_query_id'
);
}
if (empty($host_id)) {
$host_id = db_fetch_cell_prepared('SELECT host_id
FROM graph_local
WHERE id = ?',
array($local_graph_id));
}
$field = data_query_field_list(db_fetch_cell_prepared('SELECT dtd.id
FROM graph_templates_item AS gti
INNER JOIN data_template_rrd AS dtr
ON gti.task_item_id=dtr.id
INNER JOIN data_template_data AS dtd
ON dtr.local_data_id=dtd.local_data_id
WHERE gti.local_graph_id = ?
LIMIT 1',
array($local_graph_id)));
if (empty($field)) {
return;
}
if (empty($data_query_id)) {
$data_query_id = db_fetch_cell_prepared('SELECT snmp_query_id
FROM snmp_query_graph
WHERE id = ?',
array($field['output_type']));
}
$current_index = data_query_index($field['index_type'], $field['index_value'], $host_id, $data_query_id);
if ($data_query_id != 0 && $current_index != '' && $current_index != $previous_index) {
/* set the index to the new index */
db_execute_prepared('UPDATE graph_local
SET snmp_query_id = ?,
snmp_index = ?
WHERE id = ?',
array($data_query_id, $current_index, $local_graph_id));
/* update graph title cache */
update_graph_title_cache($local_graph_id);
}
}
/* update_data_source_data_query_cache - updates the local data query cache for a particular
data source
@arg $local_data_id - the id of the data source to update the data query cache for */
function update_data_source_data_query_cache($local_data_id, $host_id = '', $data_query_id = '', $previous_index = '') {
global $data_query_id_cache;
if ((!is_array($data_query_id_cache)) || cacti_sizeof($data_query_id_cache) == 0) {
$data_query_id_cache = array_rekey(
db_fetch_assoc('SELECT id, snmp_query_id
FROM snmp_query_graph'),
'id', 'snmp_query_id'
);
}
if (empty($host_id)) {
$host_id = db_fetch_cell_prepared('SELECT host_id
FROM data_local
WHERE id = ?',
array($local_data_id));
}
$field = data_query_field_list(db_fetch_cell_prepared('SELECT
data_template_data.id
FROM data_template_data
WHERE data_template_data.local_data_id = ?',
array($local_data_id)));
if (empty($field)) {
return false;
}
if (empty($data_query_id)) {
$data_query_id = db_fetch_cell_prepared('SELECT snmp_query_id
FROM snmp_query_graph
WHERE id = ?',
array($field['output_type']));
}
$current_index = data_query_index($field['index_type'], $field['index_value'], $host_id, $data_query_id);
if ($data_query_id != 0 && $current_index != '' && $current_index != $previous_index) {
/* set the index to the new index */
db_execute_prepared('UPDATE data_local
SET snmp_query_id = ?,
snmp_index = ?,
orphan = 0
WHERE id = ?',
array($data_query_id, $current_index, $local_data_id));
/* update data source title cache */
update_data_source_title_cache($local_data_id);
return true;
}
return false;
}
/* get_formatted_data_query_indexes - obtains a list of indexes for a host/data query that
is sorted by the chosen index field and formatted using the data query index title
format
@arg $host_id - the id of the host which contains the data query
@arg $data_query_id - the id of the data query to retrieve a list of indexes for
@returns - an array formatted like the following:
$arr[snmp_index] = 'formatted data query index string' */
function get_formatted_data_query_indexes($host_id, $data_query_id) {
global $config;
include_once($config['library_path'] . '/sort.php');
if (empty($data_query_id)) {
return array('' => __('Unknown Index'));
}
/* from the xml; cached in 'host_snmp_query' */
if ($host_id > 0) {
$sort_cache = db_fetch_row_prepared('SELECT sort_field, title_format
FROM host_snmp_query
WHERE host_id = ?
AND snmp_query_id = ?',
array($host_id, $data_query_id));
} else {
$sort_cache = db_fetch_row_prepared('SELECT sort_field, title_format
FROM host_snmp_query
WHERE snmp_query_id = ?
LIMIT 1',
array($data_query_id));
}
/* in case no unique index is available, fallback to first field in XML */
if (empty($sort_cache['sort_field'])) {
$snmp_queries = get_data_query_array($data_query_id);
$sort_cache = array('sort_field' => '', 'title_format' => '');
if (cacti_sizeof($snmp_queries) && isset($snmp_queries['index_order'])) {
$i = explode(':', $snmp_queries['index_order']);
if (cacti_sizeof($i) > 0) {
$sort_cache['sort_field'] = array_shift($i);
}
}
}
/* get a list of data query indexes AND the field value that we are supposed
to sort */
if ($host_id > 0) {
$sort_field_data = array_rekey(db_fetch_assoc_prepared('SELECT gl.snmp_index, hsc.field_value
FROM graph_local AS gl
INNER JOIN host_snmp_cache AS hsc
ON gl.host_id=hsc.host_id
AND gl.snmp_query_id=hsc.snmp_query_id
AND gl.snmp_index=hsc.snmp_index
WHERE gl.snmp_query_id = ?
AND gl.host_id = ?
AND hsc.field_name = ?
GROUP BY gl.snmp_index',
array($data_query_id, $host_id, $sort_cache['sort_field'])), 'snmp_index', 'field_value');
} else {
$sort_field_data = array_rekey(db_fetch_assoc_prepared('SELECT DISTINCT gl.snmp_index, hsc.field_value
FROM graph_local AS gl
INNER JOIN host_snmp_cache AS hsc
ON gl.host_id=hsc.host_id
AND gl.snmp_query_id=hsc.snmp_query_id
AND gl.snmp_index=hsc.snmp_index
WHERE gl.snmp_query_id = ?
AND hsc.field_name = ?
GROUP BY gl.snmp_index',
array($data_query_id, $sort_cache['sort_field'])), 'snmp_index', 'field_value');
}
/* sort the data using the 'data query index' sort algorithm */
uasort($sort_field_data, 'usort_data_query_index');
$sorted_results = array();
foreach ($sort_field_data as $snmp_index => $sort_field_value) {
$sorted_results[$snmp_index] = substitute_snmp_query_data($sort_cache['title_format'], $host_id, $data_query_id, $snmp_index);
}
return $sorted_results;
}
/* get_formatted_data_query_index - obtains a single index for a host/data query/data query
index that is formatted using the data query index title format
@arg $host_id - the id of the host which contains the data query
@arg $data_query_id - the id of the data query which contains the data query index
@arg $data_query_index - the index to retrieve the formatted name for
@returns - a string containing the formatted name for the given data query index */
function get_formatted_data_query_index($host_id, $data_query_id, $data_query_index) {
/* from the xml; cached in 'host_snmp_query' */
$sort_cache = db_fetch_row_prepared('SELECT sort_field, title_format
FROM host_snmp_query
WHERE host_id = ?
AND snmp_query_id = ?',
array($host_id, $data_query_id));
return substitute_snmp_query_data($sort_cache['title_format'], $host_id, $data_query_id, $data_query_index);
}
function calculate_or_set_index_order(&$raw_xml) {
if (!isset($raw_xml['index_order']) && isset($raw_xml['fields']) && is_array($raw_xml['fields'])) {
foreach($raw_xml['fields'] as $name => $attribs) {
if (isset($attribs['source']) && $attribs['source'] == 'index') {
$raw_xml['index_order'] = $name;
break;
}
}
}
}
/* get_ordered_index_type_list - builds an ordered list of data query index types that are
valid given a list of data query indexes that will be checked against the data query
cache
@arg $host_id - the id of the host which contains the data query
@arg $data_query_id - the id of the data query to build the type list from
@returns - an array of data query types either ordered or unordered depending on whether
the xml file has a manual ordering preference specified */
function get_ordered_index_type_list($host_id, $data_query_id) {
$raw_xml = get_data_query_array($data_query_id);
/* invalid xml check */
if ((!is_array($raw_xml)) || (cacti_sizeof($raw_xml) == 0)) {
return array();
}
$must_be_numeric = false;
$must_be_alpha = false;
$order_found = true;
$order_unknown = false;
$nonunique = false;
$oid_index = false;
$avail_indexes = array();
if (isset($raw_xml['oid_index'])) {
$oid_index = $raw_xml['oid_index'];
}
if (isset($raw_xml['index_order_type'])) {
switch ($raw_xml['index_order_type']) {
case 'numeric':
$mustbenumeric = true;
break;
case 'alpha':
case 'alphabetic':
$mustbealpha = true;
break;
case 'alphanumeric':
break;
default:
cacti_log('WARNING: Unknown index_order_type of ' . $raw_xml['index_order_type'] . " found for Device[$host_id], DQ[$data_query_id]. Permitted types are [alpha:numeric:alphanumeric]. Data collection can be impacted.", false, 'REINDEX');
$order_unknown = true;
}
if (isset($raw_xml['index_order'])) {
$avail_indexes = explode(':', $raw_xml['index_order']);
}
if (read_config_option('data_source_trace') == 'on') {
cacti_log("Available Sort Fields for Re-Index for Device[$host_id], DQ[$data_query_id] are [" . $raw_xml['index_order'] . "]", false, 'DSTRACE');
}
} elseif (!isset($raw_xml['arg_index'])) {
cacti_log("WARNING: Missing index_order_type XML tag for Device[$host_id], DQ[$data_query_id]. Permitted types [alpha:numeric:alphanumeric]. If a monitored object changes it's index, those changes will not be detected.", false, 'REINDEX');
$order_found = false;
}
if (cacti_sizeof($avail_indexes) == 1) {
if (read_config_option('data_source_trace') == 'on') {
cacti_log("Only One possible Sort Field found during Re-Index for Device[$host_id], DQ[$data_query_id]", false, 'DSTRACE');
}
return $avail_indexes;
}
$xml_outputs = array();
/* check for nonunique query parameter, set value */
if (isset($raw_xml['index_type']) && $raw_xml['index_type'] == 'nonunique') {
$nonunique = true;
if (read_config_option('data_source_trace') == 'on') {
cacti_log("Non-Uniqueue Specified during Re-Index for Device[$host_id], DQ[$data_query_id]", false, 'DSTRACE');
}
}
$num_indexes = db_fetch_cell_prepared('SELECT COUNT(DISTINCT snmp_index)
FROM host_snmp_cache
WHERE host_id = ?
AND snmp_query_id = ?',
array($host_id, $data_query_id));
/* list each of the input fields for this snmp query */
foreach ($raw_xml['fields'] as $field_name => $field_array) {
if ($field_array['direction'] == 'input' || $field_array['direction'] == 'input-output') {
if (isset($field_array['oid']) && $oid_index == $field_array['oid']) {
// this is a suitable sort_field
if (read_config_option('data_source_trace') == 'on') {
cacti_log("Field Name '$field_name' is an SNMP index and suitable Re-Index for Device[$host_id], DQ[$data_query_id]", false, 'DSTRACE');
}
array_push($xml_outputs, $field_name);
continue;
} elseif ($order_found && array_search($field_name, $avail_indexes) === false) {
// This is not a suitable index field
if (read_config_option('data_source_trace') == 'on') {
cacti_log("Field Name '$field_name' found not suitable during Re-Index for Device[$host_id], DQ[$data_query_id]", false, 'DSTRACE');
}
continue;
}
// Check for a non-unique sort field
$unique = db_fetch_cell_prepared('SELECT COUNT(*)
FROM (
SELECT field_value, COUNT(*) AS totals
FROM host_snmp_cache
WHERE host_id = ?
AND snmp_query_id = ?
AND field_name = ?
GROUP BY field_value
HAVING totals > 1
) AS totals',
array($host_id, $data_query_id, $field_name));
$total_unique = db_fetch_cell_prepared('SELECT COUNT(DISTINCT field_value)
FROM host_snmp_cache
WHERE host_id = ?
AND snmp_query_id = ?
AND field_name = ?',
array($host_id, $data_query_id, $field_name));
/* If the entries are not unique and non-unique is
* not specified, skip this sort field
*/
if ($unique > 0 && !$nonunique) {
if (read_config_option('data_source_trace') == 'on') {
cacti_log("Field Name '$field_name' found not suitable. Non-unique and nonunique not specified during Re-Index for Device[$host_id], DQ[$data_query_id]", false, 'DSTRACE');
}
continue;
}
if ($total_unique != $num_indexes) {
if (read_config_option('data_source_trace') == 'on') {
cacti_log("Field Name '$field_name' found not suitable. The Sort field does not have sufficient values for Device[$host_id], DQ[$data_query_id]", false, 'DSTRACE');
}
continue;
}
if ($must_be_alpha || $must_be_numeric) {
/* create a list of all values for this index */
$field_values = db_fetch_assoc_prepared('SELECT field_value
FROM host_snmp_cache
WHERE host_id = ?
AND snmp_query_id = ?
AND field_name = ?',
array($host_id, $data_query_id, $field_name));
if (cacti_sizeof($field_values)) {
foreach ($field_values as $value) {
if ($must_be_numeric && !is_numeric($value)) {
if (read_config_option('data_source_trace') == 'on') {
cacti_log("Field Name '$field_name' found not suitable. Must be numeric and non-numeric data found during Re-Index for Device[$host_id], DQ[$data_query_id]", false, 'DSTRACE');
}
continue;
} elseif ($must_be_alpha && is_numeric($value)) {
if (read_config_option('data_source_trace') == 'on') {
cacti_log("Field Name '$field_name' found not suitable. Must be alphabetic and alphabetic data found during Re-Index for Device[$host_id], DQ[$data_query_id]", false, 'DSTRACE');
}
continue;
}
}
}
}
if (read_config_option('data_source_trace') == 'on') {
cacti_log("Field Name '$field_name' found suitable during Re-Index for Device[$host_id], DQ[$data_query_id]", false, 'DSTRACE');
}
array_push($xml_outputs, $field_name);
}
}
$return_array = array();
// Reorder the sort fields by desired order
if (cacti_sizeof($avail_indexes)) {
foreach($avail_indexes as $index) {
if (array_search($index, $xml_outputs) !== false) {
$return_array[] = $index;
}
}
} else {
$return_array = $xml_outputs;
}
return $return_array;
}
/* update_data_query_sort_cache - updates the sort cache for a particular host/data query
combination. this works by fetching a list of valid data query index types and choosing
the first one in the list. the user can optionally override how the cache is updated
in the data query xml file
@arg $host_id - the id of the host which contains the data query
@arg $data_query_id - the id of the data query update the sort cache for */
function update_data_query_sort_cache($host_id, $data_query_id) {
$raw_xml = get_data_query_array($data_query_id);
/* get a list of valid data query types */
$valid_index_types = get_ordered_index_type_list($host_id, $data_query_id);
/* something is probably wrong with the data query */
if (cacti_sizeof($valid_index_types) == 0) {
$sort_field = '';
return false;
} else {
/* grab the first field off the list */
$sort_field = $valid_index_types[0];
}
/* substitute variables */
if (cacti_sizeof($raw_xml) && isset($raw_xml['index_title_format'])) {
$title_format = str_replace('|chosen_order_field|', "|query_$sort_field|", $raw_xml['index_title_format']);
} else {
$title_format = "|query_$sort_field|";
}
/* update the cache */
/* TODO: if both $sort field and $title_format are empty, this yields funny results */
db_execute_prepared('UPDATE host_snmp_query
SET sort_field = ?,
title_format = ?
WHERE host_id = ?
AND snmp_query_id = ?',
array($sort_field, $title_format, $host_id, $data_query_id));
return $sort_field;
}
/* update_data_query_sort_cache_by_host - updates the sort cache for all data queries associated
with a particular host. see update_data_query_sort_cache() for details about updating the cache
@arg $host_id - the id of the host to update the cache for */
function update_data_query_sort_cache_by_host($host_id) {
$data_queries = db_fetch_assoc_prepared('SELECT snmp_query_id
FROM host_snmp_query
WHERE host_id = ?',
array($host_id));
if (cacti_sizeof($data_queries) > 0) {
foreach ($data_queries as $data_query) {
update_data_query_sort_cache($host_id, $data_query['snmp_query_id']);
}
}
}
/* get_best_data_query_index_type - returns the best available data query index type using the
sort cache
@arg $host_id - the id of the host which contains the data query
@arg $data_query_id - the id of the data query to fetch the best data query index type for
@returns - a string containing best data query index type. this will be one of the
valid input field names as specified in the data query xml file */
function get_best_data_query_index_type($host_id, $data_query_id) {
$index_type = db_fetch_cell_prepared('SELECT sort_field
FROM host_snmp_query
WHERE host_id = ?
AND snmp_query_id = ?',
array($host_id, $data_query_id));
if ($index_type == '') {
$raw_xml = get_data_query_array($data_query_id);
if (cacti_sizeof($raw_xml)) {
if (isset($raw_xml['index_order']) && $raw_xml['index_order'] != '') {
$order = explode(':', $raw_xml['index_order']);
db_execute_prepared('UPDATE host_snmp_query
SET sort_field = ?
WHERE host_id = ?
AND snmp_query_id = ?',
array($order[0], $host_id, $data_query_id));
return $order[0];
} elseif (isset($raw_xml['fields']) && cacti_sizeof($raw_xml['fields'])) {
foreach($raw_xml['fields'] as $key => $attribs) {
break;
}
db_execute_prepared('UPDATE host_snmp_query
SET sort_field = ?
WHERE host_id = ?
AND snmp_query_id = ?',
array($key, $host_id, $data_query_id));
return $key;
} else {
return false;
}
} else {
cacti_log('ERROR: Cacti Data Query DQ[' . $data_query_id . '] XML file may be missing or not readable.');
return false;
}
}
return $index_type;
}
/* get_script_query_path - builds the complete script query executable path
@arg $args - the variable that contains any arguments to be appended to the argument
list (variables will be substituted in this function)
@arg $script_path - the path on the disk to the script file
@arg $host_id - the id of the host that this script query belongs to
@returns - a full path to the script query script containing all arguments */
function get_script_query_path($args, $script_path, $host_id) {
global $config;
include_once($config['library_path'] . '/variables.php');
/* get any extra arguments that need to be passed to the script */
if ($args != '') {
$parts = preg_split("/[\s,]*\\\"([^\\\"]+)\\\"[\s,]*|" . "[\s,]*'([^']+)'[\s,]*|" . "[\s,]+/", $args, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$extra_arguments = '';
foreach ($parts as $index => $part) {
$extra_arguments .= ($index > 0 ? ' ':'') . cacti_escapeshellarg(substitute_host_data($part, '|', '|', $host_id));
}
} else {
$extra_arguments = '';
}
/* get a complete path for out target script */
return substitute_script_query_path($script_path) . ' ' . $extra_arguments;
}
/**
* verify a given index_order
* @param array $raw_xml - parsed XML array
*
* @return bool - index_order field valid
*/
function verify_index_order($raw_xml) {
/* invalid xml check */
if ((!is_array($raw_xml)) || (cacti_sizeof($raw_xml) == 0)) {
query_debug_timer_offset('data_query', __esc('Error parsing XML file into an array.'));
return false;
}
$xml_inputs = array();
/* list each of the input fields for this snmp query */
foreach ($raw_xml['fields'] as $field_name => $field_array) {
if (is_array($field_array) && isset($field_array['direction'])) {
if ($field_array['direction'] == 'input' || $field_array['direction'] == 'input-output') {
/* create a list of all values for this index */
array_push($xml_inputs, $field_name);
}
}
}
$all_index_order_fields_found = true;
/* the xml file contains an ordered list of 'indexable' fields */
if (isset($raw_xml['index_order'])) {
$index_order_array = explode(':', $raw_xml['index_order']);
foreach ($index_order_array as $element) {
$all_index_order_fields_found = $all_index_order_fields_found && (in_array($element, $xml_inputs));
}
} else {
/* the xml file does not contain an index order */
}
return $all_index_order_fields_found;
}
/**
* perform sql updates for all required tables for new index_sort_order
* @arg array $snmp_query_array
* host_id, snmp_query_id, snmp_index_on, snmp_query_graph_id,
* snmp_index,data_template_data_id, local_data_id
*
* this code stems from lib/template.php, function create_complete_graph_from_template
*/
function update_snmp_index_order($local_data) {
if (cacti_sizeof($local_data)) {
$data_input_field = array_rekey(db_fetch_assoc_prepared("SELECT
data_input_fields.id,
data_input_fields.type_code
FROM snmp_query
INNER JOIN data_input
ON snmp_query.data_input_id = data_input.id
INNER JOIN data_input_fields
ON data_input.id = data_input_fields.data_input_id
WHERE data_input_fields.type_code IN('index_type', 'index_value', 'output_type')
AND snmp_query.id = ?",
array($local_data['snmp_query_id'])), 'type_code', 'id');
$snmp_cache_value = db_fetch_cell_prepared('SELECT field_value
FROM host_snmp_cache
WHERE host_id = ?
AND snmp_query_id = ?
AND field_name = ?
AND snmp_index = ?',
array($local_data['host_id'], $local_data['snmp_query_id'], $local_data['snmp_index_on'], $local_data['snmp_index']));
/* only update data source index if there actually *is* an index returned from host_snmp_cache */
if (!empty($snmp_cache_value)) {
db_execute_prepared('REPLACE INTO data_input_data
(data_input_field_id, data_template_data_id, t_value, value)
VALUES
(?, ?, "", ?),
(?, ?, "", ?),
(?, ?, "", ?)',
array(
/* save the value to index on (ie. ifindex, ifip, etc) */
$data_input_field['index_type'], $local_data['data_template_data_id'], $local_data['snmp_index_on'],
/* save the actual value (ie. 3, 192.168.1.101, etc) */
$data_input_field['index_value'], $local_data['data_template_data_id'], $snmp_cache_value,
/* set the expected output type (ie. bytes, errors, packets) */
$data_input_field['output_type'], $local_data['data_template_data_id'], $local_data['snmp_query_graph_id']
)
);
}
/* now that we have put data into the 'data_input_data' table, update the snmp cache for ds's */
//update_data_source_data_query_cache($local_data['local_data_id']);
}
}
/**
* verify that a Data Query Graph Template is properly mapped
* @param int $snmp_query_graph_id - the snmp query graph id
* @param array $post - the save post data
* @return bool - whether the check passed or not
*/
function api_data_query_errors($snmp_query_graph_id, $post) {
$graph_template_id = db_fetch_cell_prepared('SELECT gt.id
FROM graph_templates AS gt
INNER JOIN snmp_query_graph AS sqg
ON gt.id = sqg.graph_template_id
WHERE sqg.id = ?',
array($snmp_query_graph_id));
$data_sources = db_fetch_assoc_prepared('SELECT DISTINCT dtr.id, dtr.data_source_name, dtr.data_template_id
FROM data_template_rrd AS dtr
INNER JOIN graph_templates_item AS gti
ON dtr.id = gti.task_item_id
WHERE dtr.local_data_id = 0
AND gti.graph_template_id = ?',
array($graph_template_id));
$errors = false;
if (cacti_sizeof($data_sources)) {
foreach($data_sources as $ds) {
if (!isset($post['dsdt_' . $ds['data_template_id'] . '_' . $ds['id'] . '_check'])) {
raise_message('mapping_error', __('You must select an XML output column for Data Source \'%s\' and toggle the checkbox to its right', $ds['data_source_name']), MESSAGE_LEVEL_ERROR);
$errors = true;
}
}
} else {
raise_message('assign_error', __('Your Graph Template has not Data Templates in use. Please correct your Graph Template'), MESSAGE_LEVEL_ERROR);
$errors = true;
}
return $errors;
}
/**
* data_query_duplicate - Duplicate a Data query
*
* @param int - The original Data Query id
* @param string - The new name of the Data Query
*
* @return int|false - The id of the new data query or false on failure
*/
function data_query_duplicate($_data_query_id, $data_query_name) {
$data_query = db_fetch_row_prepared('SELECT *
FROM snmp_query
WHERE id = ?',
array($_data_query_id));
if (!cacti_sizeof($data_query)) {
return false;
}
$data_query_name = str_replace('<dataquery_name>', $data_query['name'], $data_query_name);
$save = $data_query;
$save['id'] = 0;
$save['hash'] = generate_hash();
$save['name'] = $data_query_name;
$data_query_id = sql_save($save, 'snmp_query');
$_snmp_query_graph = db_fetch_assoc_prepared('SELECT *
FROM snmp_query_graph
WHERE snmp_query_id = ?',
array($_data_query_id));
if (cacti_sizeof($_snmp_query_graph)) {
foreach($_snmp_query_graph as $_sqg) {
$save = $_sqg;
$save['id'] = 0;
$save['hash'] = generate_hash();
$save['snmp_query_id'] = $data_query_id;
$sqg_id = sql_save($save, 'snmp_query_graph');
$_snmp_query_graph_rrd = db_fetch_assoc_prepared('SELECT *
FROM snmp_query_graph_rrd
WHERE snmp_query_graph_id = ?',
array($_sqg['id']));
if (cacti_sizeof($_snmp_query_graph_rrd)) {
foreach($_snmp_query_graph_rrd as $_sqgr) {
$save = $_sqgr;
$save['snmp_query_graph_id'] = $sqg_id;
sql_save($save, 'snmp_query_graph_rrd');
}
}
$_snmp_query_graph_rrd_sv = db_fetch_assoc_prepared('SELECT *
FROM snmp_query_graph_rrd_sv
WHERE snmp_query_graph_id = ?',
array($_sqg['id']));
if (cacti_sizeof($_snmp_query_graph_rrd_sv)) {
foreach($_snmp_query_graph_rrd_sv as $_sqgrs) {
$save = $_sqgrs;
$save['id'] = 0;
$save['hash'] = generate_hash();
$save['snmp_query_graph_id'] = $sqg_id;
sql_save($save, 'snmp_query_graph_rrd_sv');
}
}
$_snmp_query_graph_sv = db_fetch_assoc_prepared('SELECT *
FROM snmp_query_graph_sv
WHERE snmp_query_graph_id = ?',
array($_sqg['id']));
if (cacti_sizeof($_snmp_query_graph_sv)) {
foreach($_snmp_query_graph_sv as $_sqgs) {
$save = $_sqgs;
$save['id'] = 0;
$save['hash'] = generate_hash();
$save['snmp_query_graph_id'] = $sqg_id;
sql_save($save, 'snmp_query_graph_sv');
}
}
}
return $data_query_id;
}
}
|