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
|
<?php
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
function condition_operator2str($operator = null) {
$operators = [
CONDITION_OPERATOR_EQUAL => _('equals'),
CONDITION_OPERATOR_NOT_EQUAL => _('does not equal'),
CONDITION_OPERATOR_LIKE => _('contains'),
CONDITION_OPERATOR_NOT_LIKE => _('does not contain'),
CONDITION_OPERATOR_IN => _('in'),
CONDITION_OPERATOR_MORE_EQUAL => _('is greater than or equals'),
CONDITION_OPERATOR_LESS_EQUAL => _('is less than or equals'),
CONDITION_OPERATOR_NOT_IN => _('not in'),
CONDITION_OPERATOR_YES => _('Yes'),
CONDITION_OPERATOR_NO => _('No'),
CONDITION_OPERATOR_REGEXP => _('matches'),
CONDITION_OPERATOR_NOT_REGEXP => _('does not match')
];
return $operator !== null
? $operators[$operator]
: $operators;
}
function condition_type2str($type = null) {
$types = [
ZBX_CONDITION_TYPE_SUPPRESSED => _('Problem is suppressed'),
ZBX_CONDITION_TYPE_EVENT_NAME => _('Event name'),
ZBX_CONDITION_TYPE_TRIGGER_SEVERITY => _('Trigger severity'),
ZBX_CONDITION_TYPE_TRIGGER => _('Trigger'),
ZBX_CONDITION_TYPE_HOST_NAME => _('Host name'),
ZBX_CONDITION_TYPE_HOST_GROUP => _('Host group'),
ZBX_CONDITION_TYPE_TEMPLATE => _('Template'),
ZBX_CONDITION_TYPE_HOST => _('Host'),
ZBX_CONDITION_TYPE_TIME_PERIOD => _('Time period'),
ZBX_CONDITION_TYPE_DRULE => _('Discovery rule'),
ZBX_CONDITION_TYPE_DCHECK => _('Discovery check'),
ZBX_CONDITION_TYPE_DOBJECT => _('Discovery object'),
ZBX_CONDITION_TYPE_DHOST_IP => _('Host IP'),
ZBX_CONDITION_TYPE_DSERVICE_TYPE => _('Service type'),
ZBX_CONDITION_TYPE_DSERVICE_PORT => _('Service port'),
ZBX_CONDITION_TYPE_DSTATUS => _('Discovery status'),
ZBX_CONDITION_TYPE_DUPTIME => _('Uptime/Downtime'),
ZBX_CONDITION_TYPE_DVALUE => _('Received value'),
ZBX_CONDITION_TYPE_EVENT_ACKNOWLEDGED => _('Event acknowledged'),
ZBX_CONDITION_TYPE_PROXY => _('Proxy'),
ZBX_CONDITION_TYPE_EVENT_TYPE => _('Event type'),
ZBX_CONDITION_TYPE_HOST_METADATA => _('Host metadata'),
ZBX_CONDITION_TYPE_EVENT_TAG => _('Tag name'),
ZBX_CONDITION_TYPE_EVENT_TAG_VALUE => _('Tag value'),
ZBX_CONDITION_TYPE_SERVICE => _('Service'),
ZBX_CONDITION_TYPE_SERVICE_NAME => _('Service name')
];
return $type !== null
? $types[$type]
: $types;
}
function discovery_object2str($object = null) {
$objects = [
EVENT_OBJECT_DHOST => _('Device'),
EVENT_OBJECT_DSERVICE => _('Service')
];
if ($object === null) {
return $objects;
}
return $objects[$object];
}
/**
* Converts numerical action condition values to their corresponding string values according to action condition type.
*
* For action condition types such as: host groups, hosts, templates, triggers, discovery rules, discovery checks,
* proxies and services, action condition values contain IDs. All unique IDs are first collected and then queried.
* For other action condition types values are returned as they are or converted using simple string conversion
* functions according to action condition type.
*
* @param array $actions Array of actions.
* array $action['filter'] Array containing arrays of action conditions and other data.
* array $action['filter']['conditions'] Array of action conditions.
*
* @return array Returns an array of actions condition string values.
*/
function actionConditionValueToString(array $actions): array {
$result = [];
$groupids = [];
$triggerids = [];
$hostids = [];
$templateids = [];
$proxyids = [];
$druleids = [];
$dcheckids = [];
$serviceids = [];
foreach ($actions as $i => $action) {
$result[$i] = [];
foreach ($action['filter']['conditions'] as $j => $condition) {
// Unknown types and all the default values for other types are 'Unknown'.
$result[$i][$j] = _('Unknown');
switch ($condition['conditiontype']) {
case ZBX_CONDITION_TYPE_HOST_GROUP:
if ($condition['value'] != 0) {
$groupids[$condition['value']] = $condition['value'];
}
else {
$result[$i][$j] = _('Deleted host group');
}
break;
case ZBX_CONDITION_TYPE_TRIGGER:
if ($condition['value'] != 0) {
$triggerids[$condition['value']] = $condition['value'];
}
else {
$result[$i][$j] = _('Deleted trigger');
}
break;
case ZBX_CONDITION_TYPE_HOST:
if ($condition['value'] != 0) {
$hostids[$condition['value']] = $condition['value'];
}
else {
$result[$i][$j] = _('Deleted host');
}
break;
case ZBX_CONDITION_TYPE_TEMPLATE:
$templateids[$condition['value']] = $condition['value'];
break;
case ZBX_CONDITION_TYPE_PROXY:
$proxyids[$condition['value']] = $condition['value'];
break;
case ZBX_CONDITION_TYPE_SERVICE:
$serviceids[$condition['value']] = $condition['value'];
break;
// return values as is for following condition types
case ZBX_CONDITION_TYPE_EVENT_NAME:
case ZBX_CONDITION_TYPE_HOST_METADATA:
case ZBX_CONDITION_TYPE_HOST_NAME:
case ZBX_CONDITION_TYPE_TIME_PERIOD:
case ZBX_CONDITION_TYPE_DHOST_IP:
case ZBX_CONDITION_TYPE_DSERVICE_PORT:
case ZBX_CONDITION_TYPE_DUPTIME:
case ZBX_CONDITION_TYPE_DVALUE:
case ZBX_CONDITION_TYPE_EVENT_TAG:
case ZBX_CONDITION_TYPE_EVENT_TAG_VALUE:
case ZBX_CONDITION_TYPE_SERVICE_NAME:
$result[$i][$j] = $condition['value'];
break;
case ZBX_CONDITION_TYPE_EVENT_ACKNOWLEDGED:
$result[$i][$j] = $condition['value'] ? _('Ack') : _('Not Ack');
break;
case ZBX_CONDITION_TYPE_TRIGGER_SEVERITY:
$result[$i][$j] = CSeverityHelper::getName((int) $condition['value']);
break;
case ZBX_CONDITION_TYPE_DRULE:
$druleids[$condition['value']] = $condition['value'];
break;
case ZBX_CONDITION_TYPE_DCHECK:
$dcheckids[$condition['value']] = $condition['value'];
break;
case ZBX_CONDITION_TYPE_DOBJECT:
$result[$i][$j] = discovery_object2str($condition['value']);
break;
case ZBX_CONDITION_TYPE_DSERVICE_TYPE:
$result[$i][$j] = discovery_check_type2str($condition['value']);
break;
case ZBX_CONDITION_TYPE_DSTATUS:
$result[$i][$j] = discovery_object_status2str($condition['value']);
break;
case ZBX_CONDITION_TYPE_EVENT_TYPE:
$result[$i][$j] = eventType($condition['value']);
break;
}
}
}
$groups = [];
$triggers = [];
$hosts = [];
$templates = [];
$proxies = [];
$drules = [];
$dchecks = [];
$services = [];
if ($groupids) {
$groups = API::HostGroup()->get([
'output' => ['name'],
'groupids' => $groupids,
'preservekeys' => true
]);
}
if ($triggerids) {
$triggers = API::Trigger()->get([
'output' => ['description'],
'triggerids' => $triggerids,
'expandDescription' => true,
'selectHosts' => ['name'],
'preservekeys' => true
]);
}
if ($hostids) {
$hosts = API::Host()->get([
'output' => ['name'],
'hostids' => $hostids,
'preservekeys' => true
]);
}
if ($templateids) {
$templates = API::Template()->get([
'output' => ['name'],
'templateids' => $templateids,
'preservekeys' => true
]);
}
if ($proxyids) {
$proxies = API::Proxy()->get([
'output' => ['name'],
'proxyids' => $proxyids,
'preservekeys' => true
]);
}
if ($druleids) {
$drules = API::DRule()->get([
'output' => ['name'],
'druleids' => $druleids,
'preservekeys' => true
]);
}
if ($dcheckids) {
$dchecks = API::DCheck()->get([
'output' => ['type', 'key_', 'ports', 'allow_redirect'],
'dcheckids' => $dcheckids,
'selectDRules' => ['name'],
'preservekeys' => true
]);
}
if ($serviceids) {
$services = API::Service()->get([
'output' => ['name'],
'serviceids' => $serviceids,
'preservekeys' => true
]);
}
if ($groups || $triggers || $hosts || $templates || $proxies || $drules || $dchecks || $services) {
foreach ($actions as $i => $action) {
foreach ($action['filter']['conditions'] as $j => $condition) {
$id = $condition['value'];
switch ($condition['conditiontype']) {
case ZBX_CONDITION_TYPE_HOST_GROUP:
if (array_key_exists($id, $groups)) {
$result[$i][$j] = $groups[$id]['name'];
}
break;
case ZBX_CONDITION_TYPE_TRIGGER:
if (array_key_exists($id, $triggers)) {
$host = reset($triggers[$id]['hosts']);
$result[$i][$j] = $host['name'].NAME_DELIMITER.$triggers[$id]['description'];
}
break;
case ZBX_CONDITION_TYPE_HOST:
if (array_key_exists($id, $hosts)) {
$result[$i][$j] = $hosts[$id]['name'];
}
break;
case ZBX_CONDITION_TYPE_TEMPLATE:
if (array_key_exists($id, $templates)) {
$result[$i][$j] = $templates[$id]['name'];
}
break;
case ZBX_CONDITION_TYPE_PROXY:
if (array_key_exists($id, $proxies)) {
$result[$i][$j] = $proxies[$id]['name'];
}
break;
case ZBX_CONDITION_TYPE_DRULE:
if (array_key_exists($id, $drules)) {
$result[$i][$j] = $drules[$id]['name'];
}
break;
case ZBX_CONDITION_TYPE_DCHECK:
if (array_key_exists($id, $dchecks)) {
$drule = reset($dchecks[$id]['drules']);
$type = $dchecks[$id]['type'];
$key_ = $dchecks[$id]['key_'];
$ports = $dchecks[$id]['ports'];
$allow_redirect = $dchecks[$id]['allow_redirect'];
$dcheck = discovery_check2str($type, $key_, $ports, $allow_redirect);
$result[$i][$j] = $drule['name'].NAME_DELIMITER.$dcheck;
}
break;
case ZBX_CONDITION_TYPE_SERVICE:
if (array_key_exists($id, $services)) {
$result[$i][$j] = $services[$id]['name'];
}
break;
}
}
}
}
return $result;
}
/**
* Returns the HTML representation of an action condition and action operation condition.
*
* @param string $condition_type
* @param string $operator
* @param string $value
* @param string $value2
*
* @return array|string
*/
function getConditionDescription($condition_type, $operator, $value, $value2) {
if ($condition_type == ZBX_CONDITION_TYPE_EVENT_TAG_VALUE) {
$description = [_('Value of tag')];
$description[] = ' ';
$description[] = italic($value2);
$description[] = ' ';
}
elseif ($condition_type == ZBX_CONDITION_TYPE_SUPPRESSED) {
return ($operator == CONDITION_OPERATOR_YES)
? [_('Problem is suppressed')]
: [_('Problem is not suppressed')];
}
elseif ($condition_type == ZBX_CONDITION_TYPE_EVENT_ACKNOWLEDGED) {
return $value ? _('Event is acknowledged') : _('Event is not acknowledged');
}
else {
$description = [condition_type2str($condition_type)];
$description[] = ' ';
}
$description[] = condition_operator2str($operator);
$description[] = ' ';
$description[] = italic($value);
return $description;
}
/**
* Gathers operation data and processes it based on operation type.
*
* @param array $operations Array of operations.
*
* @return array Returns an array of processed data.
*/
function getActionOperationData(array $operations): array {
$result = [];
$data = [];
foreach ($operations as $operation) {
switch ($operation['operationtype']) {
case OPERATION_TYPE_MESSAGE:
if ($operation['opmessage']['mediatypeid'] != 0) {
$data['mediatypeids'][$operation['opmessage']['mediatypeid']] = true;
}
if (array_key_exists('opmessage_usr', $operation) && $operation['opmessage_usr']) {
foreach ($operation['opmessage_usr'] as $users) {
$data['userids'][$users['userid']] = true;
}
}
if (array_key_exists('opmessage_grp', $operation) && $operation['opmessage_grp']) {
foreach ($operation['opmessage_grp'] as $user_groups) {
$data['usrgrpids'][$user_groups['usrgrpid']] = true;
}
}
break;
case OPERATION_TYPE_COMMAND:
if (array_key_exists('opcommand_hst', $operation) && $operation['opcommand_hst']) {
foreach ($operation['opcommand_hst'] as $host) {
if ($host['hostid'] != 0) {
$data['hostids'][$host['hostid']] = true;
}
}
}
if (array_key_exists('opcommand_grp', $operation) && $operation['opcommand_grp']) {
foreach ($operation['opcommand_grp'] as $host_group) {
$data['groupids'][$host_group['groupid']] = true;
}
}
$data['scriptids'][$operation['opcommand']['scriptid']] = true;
break;
case OPERATION_TYPE_GROUP_ADD:
case OPERATION_TYPE_GROUP_REMOVE:
if (array_key_exists('opgroup', $operation)) {
foreach ($operation['opgroup'] as $groupid) {
$data['groupids'][$groupid['groupid']] = true;
}
}
break;
case OPERATION_TYPE_TEMPLATE_ADD:
case OPERATION_TYPE_TEMPLATE_REMOVE:
foreach ($operation['optemplate'] as $templateid) {
$data['templateids'][$templateid['templateid']] = true;
}
break;
}
}
if (array_key_exists('mediatypeids', $data)) {
$result['mediatypes'] = API::Mediatype()->get([
'output' => ['name'],
'mediatypeids' => array_keys($data['mediatypeids']),
'preservekeys' => true
]);
}
if (array_key_exists('userids', $data)) {
$users = API::User()->get([
'output' => ['userid', 'username', 'name', 'surname'],
'userids' => array_keys($data['userids'])
]);
foreach ($users as $user) {
$result['users'][$user['userid']]['name'] = getUserFullname($user);
}
}
if (array_key_exists('usrgrpids', $data)) {
$result['user_groups'] = API::UserGroup()->get([
'output' => ['name'],
'usrgrpids' => array_keys($data['usrgrpids']),
'preservekeys' => true
]);
}
if (array_key_exists('hostids', $data)) {
$result['hosts'] = API::Host()->get([
'output' => ['name'],
'hostids' => array_keys($data['hostids']),
'preservekeys' => true
]);
}
if (array_key_exists('groupids', $data)) {
$result['host_groups'] = API::HostGroup()->get([
'output' => ['name'],
'groupids' => array_keys($data['groupids']),
'preservekeys' => true
]);
}
if (array_key_exists('templateids', $data)) {
$result['templates'] = API::Template()->get([
'output' => ['name'],
'templateids' => array_keys($data['templateids']),
'preservekeys' => true
]);
}
if (array_key_exists('scriptids', $data)) {
$result['scripts'] = API::Script()->get([
'output' => ['name'],
'scriptids' => array_keys($data['scriptids']),
'filter' => ['scope' => ZBX_SCRIPT_SCOPE_ACTION],
'preservekeys' => true
]);
}
return $result;
}
/**
* Formats the HTML representation of action operation values according to action operation type.
*
* @param array $operations Array of operations.
* @param int $eventsource Actions eventsource.
* @param array $operation_values All processed data of operation values.
*
* @return array Returns an array of actions operation descriptions.
*/
function getActionOperationDescriptions(array $operations, int $eventsource, array $operation_values): array {
$result = [];
$mediatypes = array_key_exists('mediatypes', $operation_values) ? $operation_values['mediatypes'] : [];
$users = array_key_exists('users', $operation_values) ? $operation_values['users'] : [];
$user_groups = array_key_exists('user_groups', $operation_values) ? $operation_values['user_groups'] : [];
$hosts = array_key_exists('hosts', $operation_values) ? $operation_values['hosts'] : [];
$host_groups = array_key_exists('host_groups', $operation_values) ? $operation_values['host_groups'] : [];
$templates = array_key_exists('templates', $operation_values) ? $operation_values['templates'] : [];
$scripts = array_key_exists('scripts', $operation_values) ? $operation_values['scripts'] : [];
foreach ($operations as $i => $operation) {
switch ($operation['operationtype']) {
case OPERATION_TYPE_MESSAGE:
$mediatype = _('all media');
$mediatypeid = $operation['opmessage']['mediatypeid'];
if ($mediatypeid != 0 && array_key_exists($mediatypeid, $mediatypes)) {
$mediatype = $mediatypes[$mediatypeid]['name'];
}
if (array_key_exists('opmessage_usr', $operation) && $operation['opmessage_usr']) {
$user_names_list = [];
foreach ($operation['opmessage_usr'] as $user) {
if (array_key_exists($user['userid'], $users)){
$user_names_list[] = $users[$user['userid']]['name'];
}
}
order_result($user_names_list);
$result[$i][] = bold(_('Send message to users').': ');
$result[$i][] = [implode(', ', $user_names_list), ' ', _('via'), ' ', $mediatype];
$result[$i][] = BR();
}
if (array_key_exists('opmessage_grp', $operation) && $operation['opmessage_grp']) {
$user_groups_list = [];
foreach ($operation['opmessage_grp'] as $user_group) {
if (array_key_exists($user_group['usrgrpid'], $user_groups)) {
$user_groups_list[] = $user_groups[$user_group['usrgrpid']]['name'];
}
}
order_result($user_groups_list);
$result[$i][] = bold(_('Send message to user groups').': ');
$result[$i][] = [implode(', ', $user_groups_list), ' ', _('via'), ' ', $mediatype];
$result[$i][] = BR();
}
break;
case OPERATION_TYPE_COMMAND:
$scriptid = $operation['opcommand']['scriptid'];
if ($eventsource == EVENT_SOURCE_SERVICE) {
$result[$i][] = [
bold(_s('Run script "%1$s" on Zabbix server', $scripts[$scriptid]['name'])),
BR()
];
break;
}
$operation += ['opcommand_hst' => [], 'opcommand_grp' => []];
if (!$operation['opcommand_hst'] && !$operation['opcommand_grp']) {
$result[$i][] = [
bold(_s('Run script "%1$s" on deleted object(s)', $scripts[$scriptid]['name'])),
BR()
];
break;
}
if ($operation['opcommand_hst']) {
$host_list = [];
foreach ($operation['opcommand_hst'] as $host) {
if ($host['hostid'] == 0) {
$result[$i][] = [
bold(_s('Run script "%1$s" on current host', $scripts[$scriptid]['name'])),
BR()
];
}
elseif (array_key_exists($host['hostid'], $hosts)) {
$host_list[] = $hosts[$host['hostid']]['name'];
}
}
if ($host_list) {
order_result($host_list);
$result[$i][] = bold(
_s('Run script "%1$s" on hosts', $scripts[$scriptid]['name']).': '
);
$result[$i][] = [implode(', ', $host_list), BR()];
}
}
if ($operation['opcommand_grp']) {
$host_group_list = [];
foreach ($operation['opcommand_grp'] as $host_group) {
if (array_key_exists($host_group['groupid'], $host_groups)) {
$host_group_list[] = $host_groups[$host_group['groupid']]['name'];
}
}
order_result($host_group_list);
$result[$i][] = bold(
_s('Run script "%1$s" on host groups', $scripts[$scriptid]['name']).': '
);
$result[$i][] = [implode(', ', $host_group_list), BR()];
}
break;
case OPERATION_TYPE_HOST_ADD:
$result[$i][] = [bold(_('Add host')), BR()];
break;
case OPERATION_TYPE_HOST_REMOVE:
$result[$i][] = [bold(_('Remove host')), BR()];
break;
case OPERATION_TYPE_HOST_TAGS_ADD:
case OPERATION_TYPE_HOST_TAGS_REMOVE:
$tags = [];
if (array_key_exists('optag', $operation) && $operation['optag']) {
CArrayHelper::sort($operation['optag'], ['tag', 'value']);
foreach ($operation['optag'] as $tag) {
$value = getTagString($tag);
if ($value !== '') {
$tags[] = (new CSpan($value))
->addClass(ZBX_STYLE_TAG)
->setHint(getTagString($tag));
}
}
if ($operation['operationtype'] == OPERATION_TYPE_HOST_TAGS_ADD) {
$result[$i][] = bold(_('Add host tags').': ');
}
else {
$result[$i][] = bold(_('Remove host tags').': ');
}
}
$result[$i][] = [$tags, BR()];
break;
case OPERATION_TYPE_HOST_ENABLE:
$result[$i][] = [bold(_('Enable host')), BR()];
break;
case OPERATION_TYPE_HOST_DISABLE:
$result[$i][] = [bold(_('Disable host')), BR()];
break;
case OPERATION_TYPE_GROUP_ADD:
case OPERATION_TYPE_GROUP_REMOVE:
$host_group_list = [];
if (array_key_exists('opgroup', $operation)) {
foreach ($operation['opgroup'] as $groupid) {
if (array_key_exists($groupid['groupid'], $host_groups)) {
$host_group_list[] = $host_groups[$groupid['groupid']]['name'];
}
}
order_result($host_group_list);
}
$host_group_list = $host_group_list
? implode(', ', $host_group_list)
: italic(_('Deleted host group(s)'));
if ($operation['operationtype'] == OPERATION_TYPE_GROUP_ADD) {
$result[$i][] = bold(_('Add to host groups').': ');
}
else {
$result[$i][] = bold(_('Remove from host groups').': ');
}
$result[$i][] = [$host_group_list, BR()];
break;
case OPERATION_TYPE_TEMPLATE_ADD:
case OPERATION_TYPE_TEMPLATE_REMOVE:
$template_list = [];
foreach ($operation['optemplate'] as $templateid) {
if (array_key_exists($templateid['templateid'], $templates)) {
$template_list[] = $templates[$templateid['templateid']]['name'];
}
}
order_result($template_list);
if ($operation['operationtype'] == OPERATION_TYPE_TEMPLATE_ADD) {
$result[$i][] = bold(_('Link templates').': ');
}
else {
$result[$i][] = bold(_('Unlink templates').': ');
}
$result[$i][] = [implode(', ', $template_list), BR()];
break;
case OPERATION_TYPE_HOST_INVENTORY:
$host_inventory_modes = getHostInventoryModes();
$result[$i][] = bold(operation_type2str(OPERATION_TYPE_HOST_INVENTORY).': ');
$result[$i][] = [$host_inventory_modes[$operation['opinventory']['inventory_mode']], BR()];
break;
case OPERATION_TYPE_RECOVERY_MESSAGE:
case OPERATION_TYPE_UPDATE_MESSAGE:
$result[$i][] = bold(_('Notify all involved'));
break;
}
}
return $result;
}
/**
* Return an array of action conditions supported by the given event source.
*
* @param int|string $eventsource
*/
function get_conditions_by_eventsource($eventsource): array {
$conditions[EVENT_SOURCE_TRIGGERS] = [
ZBX_CONDITION_TYPE_EVENT_NAME,
ZBX_CONDITION_TYPE_TRIGGER,
ZBX_CONDITION_TYPE_TRIGGER_SEVERITY,
ZBX_CONDITION_TYPE_HOST,
ZBX_CONDITION_TYPE_HOST_GROUP,
ZBX_CONDITION_TYPE_SUPPRESSED,
ZBX_CONDITION_TYPE_EVENT_TAG,
ZBX_CONDITION_TYPE_EVENT_TAG_VALUE,
ZBX_CONDITION_TYPE_TEMPLATE,
ZBX_CONDITION_TYPE_TIME_PERIOD
];
$conditions[EVENT_SOURCE_DISCOVERY] = [
ZBX_CONDITION_TYPE_DHOST_IP,
ZBX_CONDITION_TYPE_DCHECK,
ZBX_CONDITION_TYPE_DOBJECT,
ZBX_CONDITION_TYPE_DRULE,
ZBX_CONDITION_TYPE_DSTATUS,
ZBX_CONDITION_TYPE_PROXY,
ZBX_CONDITION_TYPE_DVALUE,
ZBX_CONDITION_TYPE_DSERVICE_PORT,
ZBX_CONDITION_TYPE_DSERVICE_TYPE,
ZBX_CONDITION_TYPE_DUPTIME
];
$conditions[EVENT_SOURCE_AUTOREGISTRATION] = [
ZBX_CONDITION_TYPE_HOST_NAME,
ZBX_CONDITION_TYPE_HOST_METADATA,
ZBX_CONDITION_TYPE_PROXY
];
$conditions[EVENT_SOURCE_INTERNAL] = [
ZBX_CONDITION_TYPE_EVENT_TYPE,
ZBX_CONDITION_TYPE_HOST,
ZBX_CONDITION_TYPE_HOST_GROUP,
ZBX_CONDITION_TYPE_EVENT_TAG,
ZBX_CONDITION_TYPE_EVENT_TAG_VALUE,
ZBX_CONDITION_TYPE_TEMPLATE
];
$conditions[EVENT_SOURCE_SERVICE] = [
ZBX_CONDITION_TYPE_SERVICE,
ZBX_CONDITION_TYPE_SERVICE_NAME,
ZBX_CONDITION_TYPE_EVENT_TAG,
ZBX_CONDITION_TYPE_EVENT_TAG_VALUE
];
if (array_key_exists($eventsource, $conditions)) {
return $conditions[$eventsource];
}
return $conditions[EVENT_SOURCE_TRIGGERS];
}
/**
* Return allowed operations types.
*
* @param int $eventsource
*/
function getAllowedOperations($eventsource): array {
switch ($eventsource) {
case EVENT_SOURCE_TRIGGERS:
case EVENT_SOURCE_SERVICE:
return [
ACTION_OPERATION => [
OPERATION_TYPE_MESSAGE,
OPERATION_TYPE_COMMAND
],
ACTION_RECOVERY_OPERATION => [
OPERATION_TYPE_MESSAGE,
OPERATION_TYPE_COMMAND,
OPERATION_TYPE_RECOVERY_MESSAGE
],
ACTION_UPDATE_OPERATION => [
OPERATION_TYPE_MESSAGE,
OPERATION_TYPE_COMMAND,
OPERATION_TYPE_UPDATE_MESSAGE
]
];
case EVENT_SOURCE_DISCOVERY:
case EVENT_SOURCE_AUTOREGISTRATION:
return [
ACTION_OPERATION => [
OPERATION_TYPE_MESSAGE,
OPERATION_TYPE_COMMAND,
OPERATION_TYPE_HOST_ADD,
OPERATION_TYPE_HOST_REMOVE,
OPERATION_TYPE_GROUP_ADD,
OPERATION_TYPE_GROUP_REMOVE,
OPERATION_TYPE_TEMPLATE_ADD,
OPERATION_TYPE_TEMPLATE_REMOVE,
OPERATION_TYPE_HOST_TAGS_ADD,
OPERATION_TYPE_HOST_TAGS_REMOVE,
OPERATION_TYPE_HOST_ENABLE,
OPERATION_TYPE_HOST_DISABLE,
OPERATION_TYPE_HOST_INVENTORY
]
];
case EVENT_SOURCE_INTERNAL:
return [
ACTION_OPERATION => [OPERATION_TYPE_MESSAGE],
ACTION_RECOVERY_OPERATION => [
OPERATION_TYPE_MESSAGE,
OPERATION_TYPE_RECOVERY_MESSAGE
]
];
default:
return [];
}
}
/**
* Get operation type text label according $type value. If $type is equal null array of all available operation types
* will be returned.
*
* @param int|null $type Operation type, one of OPERATION_TYPE_* constant or null.
*
* @return string|array
*/
function operation_type2str($type) {
$types = [
OPERATION_TYPE_MESSAGE => _('Send message'),
OPERATION_TYPE_COMMAND => _('Remote command'),
OPERATION_TYPE_HOST_ADD => _('Add host'),
OPERATION_TYPE_HOST_REMOVE => _('Remove host'),
OPERATION_TYPE_HOST_ENABLE => _('Enable host'),
OPERATION_TYPE_HOST_DISABLE => _('Disable host'),
OPERATION_TYPE_GROUP_ADD => _('Add to host group'),
OPERATION_TYPE_GROUP_REMOVE => _('Remove from host group'),
OPERATION_TYPE_TEMPLATE_ADD => _('Link template'),
OPERATION_TYPE_TEMPLATE_REMOVE => _('Unlink template'),
OPERATION_TYPE_HOST_INVENTORY => _('Set host inventory mode'),
OPERATION_TYPE_RECOVERY_MESSAGE => _('Notify all involved'),
OPERATION_TYPE_UPDATE_MESSAGE => _('Notify all involved'),
OPERATION_TYPE_HOST_TAGS_ADD => _('Add host tags'),
OPERATION_TYPE_HOST_TAGS_REMOVE => _('Remove host tags')
];
if (is_null($type)) {
return order_result($types);
}
elseif (array_key_exists($type, $types)) {
return $types[$type];
}
else {
return _('Unknown');
}
}
function sortOperations($eventsource, &$operations): void {
if (in_array($eventsource, [EVENT_SOURCE_TRIGGERS, EVENT_SOURCE_INTERNAL, EVENT_SOURCE_SERVICE])) {
$esc_step_from = [];
$esc_step_to = [];
$esc_period = [];
$operationTypes = [];
$simple_interval_parser = new CSimpleIntervalParser();
foreach ($operations as $key => $operation) {
$esc_step_from[$key] = $operation['esc_step_from'];
$esc_step_to[$key] = $operation['esc_step_to'];
/*
* Try to sort by "esc_period" in seconds, otherwise sort as string in case it's a macro or something
* invalid.
*/
$esc_period[$key] = ($simple_interval_parser->parse($operation['esc_period']) == CParser::PARSE_SUCCESS)
? timeUnitToSeconds($operation['esc_period'])
: $operation['esc_period'];
$operationTypes[$key] = $operation['operationtype'];
}
array_multisort($esc_step_from, SORT_ASC, $esc_step_to, SORT_ASC, $esc_period, SORT_ASC, $operationTypes,
SORT_ASC, $operations
);
}
else {
$order = getAllowedOperations($eventsource)[ACTION_OPERATION];
usort($operations,
static fn($a, $b) => array_search($a['operationtype'], $order) - array_search($b['operationtype'], $order)
);
}
}
/**
* Return an array of operators supported by the given action condition.
*
* @param int $conditiontype
*/
function get_operators_by_conditiontype($conditiontype): array {
switch ($conditiontype) {
case ZBX_CONDITION_TYPE_DCHECK:
case ZBX_CONDITION_TYPE_DHOST_IP:
case ZBX_CONDITION_TYPE_DRULE:
case ZBX_CONDITION_TYPE_DSERVICE_PORT:
case ZBX_CONDITION_TYPE_DSERVICE_TYPE:
case ZBX_CONDITION_TYPE_HOST:
case ZBX_CONDITION_TYPE_HOST_GROUP:
case ZBX_CONDITION_TYPE_PROXY:
case ZBX_CONDITION_TYPE_SERVICE:
case ZBX_CONDITION_TYPE_TEMPLATE:
case ZBX_CONDITION_TYPE_TRIGGER:
return [
CONDITION_OPERATOR_EQUAL,
CONDITION_OPERATOR_NOT_EQUAL
];
case ZBX_CONDITION_TYPE_SERVICE_NAME:
case ZBX_CONDITION_TYPE_EVENT_NAME:
return [
CONDITION_OPERATOR_LIKE,
CONDITION_OPERATOR_NOT_LIKE
];
case ZBX_CONDITION_TYPE_TRIGGER_SEVERITY:
return [
CONDITION_OPERATOR_EQUAL,
CONDITION_OPERATOR_NOT_EQUAL,
CONDITION_OPERATOR_MORE_EQUAL,
CONDITION_OPERATOR_LESS_EQUAL
];
case ZBX_CONDITION_TYPE_TIME_PERIOD:
return [
CONDITION_OPERATOR_IN,
CONDITION_OPERATOR_NOT_IN
];
case ZBX_CONDITION_TYPE_SUPPRESSED:
return [
CONDITION_OPERATOR_NO,
CONDITION_OPERATOR_YES
];
case ZBX_CONDITION_TYPE_DOBJECT:
case ZBX_CONDITION_TYPE_DSTATUS:
case ZBX_CONDITION_TYPE_EVENT_ACKNOWLEDGED:
case ZBX_CONDITION_TYPE_EVENT_TYPE:
return [
CONDITION_OPERATOR_EQUAL
];
case ZBX_CONDITION_TYPE_DUPTIME:
return [
CONDITION_OPERATOR_MORE_EQUAL,
CONDITION_OPERATOR_LESS_EQUAL
];
case ZBX_CONDITION_TYPE_DVALUE:
return [
CONDITION_OPERATOR_EQUAL,
CONDITION_OPERATOR_NOT_EQUAL,
CONDITION_OPERATOR_MORE_EQUAL,
CONDITION_OPERATOR_LESS_EQUAL,
CONDITION_OPERATOR_LIKE,
CONDITION_OPERATOR_NOT_LIKE
];
case ZBX_CONDITION_TYPE_HOST_METADATA:
case ZBX_CONDITION_TYPE_HOST_NAME:
return [
CONDITION_OPERATOR_LIKE,
CONDITION_OPERATOR_NOT_LIKE,
CONDITION_OPERATOR_REGEXP,
CONDITION_OPERATOR_NOT_REGEXP
];
case ZBX_CONDITION_TYPE_EVENT_TAG:
case ZBX_CONDITION_TYPE_EVENT_TAG_VALUE:
return [
CONDITION_OPERATOR_EQUAL,
CONDITION_OPERATOR_NOT_EQUAL,
CONDITION_OPERATOR_LIKE,
CONDITION_OPERATOR_NOT_LIKE
];
default:
return [];
}
}
function count_operations_delay($operations, $def_period): array {
$delays = [1 => 0];
$periods = [];
$max_step = 0;
$simple_interval_parser = new CSimpleIntervalParser();
$def_period = CMacrosResolverHelper::resolveTimeUnitMacros(
[['def_period' => $def_period]], ['def_period']
)[0]['def_period'];
$def_period = ($simple_interval_parser->parse($def_period) == CParser::PARSE_SUCCESS)
? timeUnitToSeconds($def_period)
: null;
$operations = CMacrosResolverHelper::resolveTimeUnitMacros($operations, ['esc_period']);
foreach ($operations as $operation) {
$esc_period = ($simple_interval_parser->parse($operation['esc_period']) == CParser::PARSE_SUCCESS)
? timeUnitToSeconds($operation['esc_period'])
: null;
$esc_period = ($esc_period === null || $esc_period != 0) ? $esc_period : $def_period;
$step_to = ($operation['esc_step_to'] != 0) ? $operation['esc_step_to'] : 9999;
if ($max_step < $operation['esc_step_from']) {
$max_step = $operation['esc_step_from'];
}
for ($i = $operation['esc_step_from']; $i <= $step_to; $i++) {
if (!array_key_exists($i, $periods) || $esc_period === null || $periods[$i] > $esc_period) {
$periods[$i] = $esc_period;
}
}
}
for ($i = 1; $i <= $max_step; $i++) {
$esc_period = array_key_exists($i, $periods) ? $periods[$i] : $def_period;
$delays[$i + 1] = ($esc_period !== null && $delays[$i] !== null) ? $delays[$i] + $esc_period : null;
}
return $delays;
}
/**
* Returns the names of the "Event type" action condition values.
*
* If the $type parameter is passed, returns the name of the specific value, otherwise - returns an array of all
* supported values.
*
* @param string $type
*
* @return array|string
*/
function eventType($type = null) {
$types = [
EVENT_TYPE_ITEM_NOTSUPPORTED => _('Item in "not supported" state'),
EVENT_TYPE_LLDRULE_NOTSUPPORTED => _('Low-level discovery rule in "not supported" state'),
EVENT_TYPE_TRIGGER_UNKNOWN => _('Trigger in "unknown" state')
];
if (is_null($type)) {
return $types;
}
return $types[$type];
}
/**
* Get data required to create messages, severity changes, actions icon with popup with event actions.
*
* @param array $events Array with event objects with acknowledges.
* @param array $triggers Array of triggers.
*/
function getEventsActionsIconsData(array $events, array $triggers): array {
$suppressions = getEventsSuppressions($events);
$messages = getEventsMessages($events);
$severities = getEventsSeverityChanges($events, $triggers);
$actions = getEventsAlertsOverview($events);
return [
'data' => [
'suppressions' => $suppressions['data'],
'messages' => $messages['data'],
'severities' => $severities['data'],
'actions' => $actions
],
'userids' => $messages['userids'] + $severities['userids'] + $suppressions['userids']
];
}
/**
* Get data, required to create suppressed problem icon with popup with event suppression data.
*
* @param array $events Array with event objects with acknowledges.
* string $events[]['eventid'] Problem event ID.
* array $events[]['acknowledges'] Array with manual updates to problem.
* string $events[]['acknowledges'][]['action'] Action that was performed by problem update.
* string $events[]['acknowledges'][]['suppress_until'] Time until problem suppressed.
* string $events[]['acknowledges'][]['clock'] Time when manual suppression was made.
* string $events[]['acknowledges'][]['userid'] Author's userid.
*/
function getEventsSuppressions(array $events): array {
$suppressions = [];
$userids = [];
// Create array of suppressions for each event.
foreach ($events as $event) {
$event_suppressions = [];
foreach ($event['acknowledges'] as $ack) {
if (($ack['action'] & ZBX_PROBLEM_UPDATE_SUPPRESS) == ZBX_PROBLEM_UPDATE_SUPPRESS) {
$event_suppressions[] = [
'suppress_until' => $ack['suppress_until'],
'userid' => $ack['userid'],
'clock' => $ack['clock']
];
$userids[$ack['userid']] = true;
}
elseif (($ack['action'] & ZBX_PROBLEM_UPDATE_UNSUPPRESS) == ZBX_PROBLEM_UPDATE_UNSUPPRESS) {
$event_suppressions[] = [
'userid' => $ack['userid'],
'clock' => $ack['clock']
];
$userids[$ack['userid']] = true;
}
}
CArrayHelper::sort($event_suppressions, [['field' => 'clock', 'order' => ZBX_SORT_DOWN]]);
$suppressions[$event['eventid']] = [
'suppress_until' => array_values($event_suppressions),
'count' => count($event_suppressions)
];
}
return [
'data' => $suppressions,
'userids' => $userids
];
}
/**
* Get data, required to create messages icon with popup with event messages.
*
* @param array $events Array with event objects with acknowledges.
* string $events[]['eventid'] Problem event ID.
* array $events[]['acknowledges'] Array with manual updates to problem.
* string $events[]['acknowledges'][]['action'] Action that was performed by problem update.
* string $events[]['acknowledges'][]['message'] Message text.
* string $events[]['acknowledges'][]['clock'] Time when message was added.
* string $events[]['acknowledges'][]['userid'] Author's userid.
*/
function getEventsMessages(array $events): array {
$messages = [];
$userids = [];
// Create array of messages for each event
foreach ($events as $event) {
$event_messages = [];
foreach ($event['acknowledges'] as $ack) {
if (($ack['action'] & ZBX_PROBLEM_UPDATE_MESSAGE) == ZBX_PROBLEM_UPDATE_MESSAGE) {
$event_messages[] = [
'message' => $ack['message'],
'userid' => $ack['userid'],
'clock' => $ack['clock']
];
$userids[$ack['userid']] = true;
}
}
CArrayHelper::sort($event_messages, [['field' => 'clock', 'order' => ZBX_SORT_DOWN]]);
$messages[$event['eventid']] = [
'messages' => array_values($event_messages),
'count' => count($event_messages)
];
}
return [
'data' => $messages,
'userids' => $userids
];
}
/**
* Get data, required to create severity changes icon with popup with event severity changes.
*
* @param array $events Array with event objects with acknowledges.
* string $events[]['eventid'] Problem event ID.
* string $events[]['severity'] Current event severity.
* string $events[]['objectid'] Related trigger ID.
* array $events[]['acknowledges'] Array with manual updates to problem.
* string $events[]['acknowledges'][]['action'] Action that was performed by problem update.
* string $events[]['acknowledges'][]['clock'] Time when severity was changed.
* string $events[]['acknowledges'][]['old_severity'] Severity before the change.
* string $events[]['acknowledges'][]['new_severity'] Severity after the change.
* string $events[]['acknowledges'][]['userid'] Responsible user's userid.
* @param array $triggers Related trigger data.
* string $triggers[]['priority'] Severity of trigger.
*/
function getEventsSeverityChanges(array $events, array $triggers): array {
$severities = [];
$userids = [];
// Create array of messages for each event.
foreach ($events as $event) {
$event_severities = [];
foreach ($event['acknowledges'] as $ack) {
if (($ack['action'] & ZBX_PROBLEM_UPDATE_SEVERITY) == ZBX_PROBLEM_UPDATE_SEVERITY) {
$event_severities[] = [
'old_severity' => $ack['old_severity'],
'new_severity' => $ack['new_severity'],
'userid' => $ack['userid'],
'clock' => $ack['clock']
];
$userids[$ack['userid']] = true;
}
}
CArrayHelper::sort($event_severities, [['field' => 'clock', 'order' => ZBX_SORT_DOWN]]);
$severities[$event['eventid']] = [
'severities' => array_values($event_severities),
'count' => count($event_severities),
'original_severity' => $triggers[$event['objectid']]['priority'],
'current_severity' => $event['severity']
];
}
return [
'data' => $severities,
'userids' => $userids
];
}
/**
* Get data, required to create actions icon.
*
* @param array $events Array with event objects with acknowledges.
* string $events[]['eventid'] Problem event ID.
* string $events[]['r_eventid'] OK event ID.
*
* @return array List indexed by eventid containing overview on event alerts.
*/
function getEventsAlertsOverview(array $events): array {
$alert_eventids = [];
$actions = [];
$event_alert_state = [];
foreach ($events as $event) {
// Get alerts for event.
$alert_eventids[$event['eventid']] = true;
// Get alerts for related recovery events.
if ($event['r_eventid'] != 0) {
$alert_eventids[$event['r_eventid']] = true;
}
}
if ($alert_eventids) {
$event_alert_state = array_combine(array_keys($alert_eventids), array_fill(0, count($alert_eventids), [
'failed_cnt' => 0,
'in_progress_cnt' => 0,
'total_cnt' => 0
]));
$alerts = API::Alert()->get([
'groupCount' => true,
'countOutput' => true,
'filter' => ['status' => ALERT_STATUS_FAILED],
'eventids' => array_keys($alert_eventids)
]);
foreach ($alerts as $alert) {
$event_alert_state[$alert['eventid']]['failed_cnt'] = (int) $alert['rowscount'];
}
$alerts = API::Alert()->get([
'groupCount' => true,
'countOutput' => true,
'filter' => ['status' => [ALERT_STATUS_NEW, ALERT_STATUS_NOT_SENT]],
'eventids' => array_keys($alert_eventids)
]);
foreach ($alerts as $alert) {
$event_alert_state[$alert['eventid']]['in_progress_cnt'] = (int) $alert['rowscount'];
}
$alerts = API::Alert()->get([
'groupCount' => true,
'countOutput' => true,
'eventids' => array_keys($alert_eventids)
]);
foreach ($alerts as $alert) {
$event_alert_state[$alert['eventid']]['total_cnt'] = (int) $alert['rowscount'];
}
}
// Create array of actions for each event.
foreach ($events as $event) {
$event_actions = $event_alert_state[$event['eventid']];
if ($event['r_eventid']) {
$r_event_actions = $event_alert_state[$event['r_eventid']];
$event_actions['failed_cnt'] += $r_event_actions['failed_cnt'];
$event_actions['total_cnt'] += $r_event_actions['total_cnt'];
$event_actions['in_progress_cnt'] += $r_event_actions['in_progress_cnt'];
}
$actions[$event['eventid']] = [
'count' => $event_actions['total_cnt'] + count($event['acknowledges']),
'has_uncomplete_action' => (bool) $event_actions['in_progress_cnt'],
'has_failed_action' => (bool) $event_actions['failed_cnt']
];
}
return $actions;
}
/**
* Get data, required to create table with all (automatic and manual) actions for Event details page.
*
* @param array $event Event object with acknowledges.
* string $event['eventid'] Problem event ID.
* string $event['r_eventid'] OK event ID.
*/
function getEventDetailsActions(array $event): array {
$r_events = [];
// Select eventids for alert retrieval.
$alert_eventids = [$event['eventid']];
if ($event['r_eventid'] != 0) {
$alert_eventids[] = $event['r_eventid'];
$r_events = API::Event()->get([
'output' => ['clock'],
'eventids' => $event['r_eventid'],
'preservekeys' => true
]);
}
$search_limit = CSettingsHelper::get(CSettingsHelper::SEARCH_LIMIT);
// Get automatic actions (alerts).
$alerts = API::Alert()->get([
'output' => ['alerttype', 'clock', 'error', 'eventid', 'esc_step', 'mediatypeid', 'message', 'retries',
'sendto', 'status', 'subject', 'userid', 'p_eventid', 'acknowledgeid'
],
'eventids' => $alert_eventids,
'limit' => $search_limit
]);
$actions = getSingleEventActions($event, $r_events, $alerts);
return [
'actions' => $actions['actions'],
'mediatypeids' => $actions['mediatypeids'],
'userids' => $actions['userids'],
'count' => $actions['count']
];
}
/**
* Get array with all actions for single event.
*
* @param array $event Event objects with acknowledges.
* string $event['eventid'] Problem event ID.
* string $event['r_eventid'] OK event ID.
* string $event['clock'] Time when event occurred.
* array $event['acknowledges'] Array with manual updates to problem.
* string $event['acknowledges'][]['userid'] User ID.
* @param array $r_events Recovery event data for all requested events.
* string $r_events[]['clock'] Recovery event creation time.
* @param array $alerts Alert data for all requested alerts.
* string $alerts[]['eventid'] If of problem event for which this alert was generated.
* string $alerts[]['mediatypeid'] ID for mediatype used for alert.
* string $alerts[]['alerttype'] Type of alert.
* string $alerts[]['status'] Alert status.
* string $alerts[]['userid'] ID of alert recipient.
*/
function getSingleEventActions(array $event, array $r_events, array $alerts): array {
$action_count = 0;
$has_uncomplete_action = false;
$has_failed_action = false;
$mediatypeids = [];
$userids = [];
// Create array of automatically and manually performed actions combined.
$actions = [];
// Add row for problem generation event.
$actions[] = [
'action_type' => ZBX_EVENT_HISTORY_PROBLEM_EVENT,
'clock' => $event['clock']
];
// Add row for problem recovery event.
if (array_key_exists($event['r_eventid'], $r_events)) {
$actions[] = [
'action_type' => ZBX_EVENT_HISTORY_RECOVERY_EVENT,
'clock' => $r_events[$event['r_eventid']]['clock']
];
}
// Add manual operations.
foreach ($event['acknowledges'] as $ack) {
$ack['action_type'] = ZBX_EVENT_HISTORY_MANUAL_UPDATE;
$actions[] = $ack;
$action_count++;
$userids[$ack['userid']] = true;
}
// Add alerts.
foreach ($alerts as $alert) {
// Add only alerts, related to current event.
if (bccomp($alert['eventid'], $event['eventid']) == 0
|| bccomp($alert['eventid'], $event['r_eventid']) == 0) {
$alert['action_type'] = ZBX_EVENT_HISTORY_ALERT;
$actions[] = $alert;
$action_count++;
if ($alert['alerttype'] == ALERT_TYPE_MESSAGE) {
if ($alert['mediatypeid'] != 0) {
$mediatypeids[$alert['mediatypeid']] = true;
}
if ($alert['userid'] != 0) {
$userids[$alert['userid']] = true;
}
}
if ($alert['status'] == ALERT_STATUS_NEW || $alert['status'] == ALERT_STATUS_NOT_SENT) {
$has_uncomplete_action = true;
}
elseif ($alert['status'] == ALERT_STATUS_FAILED) {
$has_failed_action = true;
}
}
}
// Sort by action_type is done to put Recovery event before actions, resulted from it. Same for other action_type.
CArrayHelper::sort($actions, [
['field' => 'clock', 'order' => ZBX_SORT_DOWN],
['field' => 'action_type', 'order' => ZBX_SORT_DOWN],
['field' => 'alertid', 'order' => ZBX_SORT_DOWN]
]);
return [
'actions' => array_values($actions),
'count' => $action_count,
'has_uncomplete_action' => $has_uncomplete_action,
'has_failed_action' => $has_failed_action,
'mediatypeids' => $mediatypeids,
'userids' => $userids
];
}
/**
* Get data required to create history list in problem update page.
*
* @param array $event Array with event objects with acknowledges.
* array $event['acknowledges'] Array with manual updates to problem.
* string $event['acknowledges'][]['clock'] Time when severity was changed.
* string $event['acknowledges'][]['userid'] Responsible user's userid.
*/
function getEventUpdates(array $event): array {
$userids = [];
foreach ($event['acknowledges'] as $ack) {
$userids[$ack['userid']] = true;
}
CArrayHelper::sort($event['acknowledges'], [['field' => 'clock', 'order' => ZBX_SORT_DOWN]]);
return [
'data' => array_values($event['acknowledges']),
'userids' => $userids
];
}
/**
* Make icons (suppressions, messages, severity changes, actions) for actions column.
*
* @param string $eventid ID for event, for which icons are created.
* @param array $actions Array of actions data.
* array $actions['suppressions'] Suppression icon data.
* array $actions['messages'] Messages icon data.
* array $actions['severities'] Severity change icon data.
* array $actions['actions'] Actions icon data.
* @param array $users User name, surname and username.
* @param bool $is_acknowledged Is the event currently acknowledged. If true, display icon.
*
* @throws Exception
*
* @return CCol|string
*/
function makeEventActionsIcons($eventid, array $actions, array $users, bool $is_acknowledged) {
$suppression_icon = makeEventSuppressionsProblemIcon($actions['suppressions'][$eventid], $users);
$messages_icon = makeEventMessagesIcon($actions['messages'][$eventid], $users);
$severities_icon = makeEventSeverityChangesIcon($actions['severities'][$eventid], $users);
$actions_icon = makeEventActionsIcon($actions['actions'][$eventid], $eventid);
$action_icons = [];
if ($is_acknowledged) {
$action_icons[] = (new CIcon(ZBX_ICON_CHECK, _('Acknowledged')))->addClass(ZBX_STYLE_COLOR_POSITIVE);
}
if ($suppression_icon !== null) {
$action_icons[] = $suppression_icon;
}
if ($messages_icon !== null) {
$action_icons[] = $messages_icon;
}
if ($severities_icon !== null) {
$action_icons[] = $severities_icon;
}
if ($actions_icon !== null) {
$action_icons[] = $actions_icon;
}
return $action_icons ? (new CCol($action_icons))->addClass(ZBX_STYLE_NOWRAP) : '';
}
/**
* Create icon with hintbox for event suppressions.
* Records must be passed in the order starting from the latest by field 'clock'.
*
* @param array $data
* array $data['suppress_until'][]['suppress_until'] Time until problem is suppressed by user.
* string $data['suppress_until'][]['clock'] Suppression creation time.
* @param array $users User name, surname and username.
*
* @throws Exception
*/
function makeEventSuppressionsProblemIcon(array $data, array $users): ?CButtonIcon {
if ($data['count'] == 0) {
return null;
}
$table = (new CTableInfo())->setHeader([_('Time'), _('User'), _('Action'), _('Suppress until')]);
for ($i = 0; $i < $data['count'] && $i < ZBX_WIDGET_ROWS; $i++) {
$suppression = $data['suppress_until'][$i];
// Added in order to reuse makeActionTableUser().
$suppression['action_type'] = ZBX_EVENT_HISTORY_MANUAL_UPDATE;
if (array_key_exists('suppress_until', $suppression)) {
$icon = new CIcon(ZBX_ICON_EYE_OFF, _('Suppressed'));
if ($suppression['suppress_until'] == ZBX_PROBLEM_SUPPRESS_TIME_INDEFINITE) {
$suppress_until = _s('Indefinitely');
}
else {
$suppress_until = $suppression['suppress_until'] < strtotime('tomorrow')
&& $suppression['suppress_until'] > strtotime('today')
? zbx_date2str(TIME_FORMAT, $suppression['suppress_until'])
: zbx_date2str(DATE_TIME_FORMAT, $suppression['suppress_until']);
}
}
else {
$icon = new CIcon(ZBX_ICON_EYE, _('Unsuppressed'));
$suppress_until = '';
}
$table->addRow([
zbx_date2str(DATE_TIME_FORMAT_SECONDS, $suppression['clock']),
makeActionTableUser($suppression, $users),
$icon,
$suppress_until
]);
}
if ($data['count'] > ZBX_WIDGET_ROWS) {
$table->setFooter(
(new CCol(_s('Displaying %1$s of %2$s found', ZBX_WIDGET_ROWS, $data['count'])))
->addClass(ZBX_STYLE_LIST_TABLE_FOOTER)
);
}
return (new CButtonIcon(array_key_exists('suppress_until', $data['suppress_until'][0])
? ZBX_ICON_EYE_OFF
: ZBX_ICON_EYE
))
->addClass(ZBX_STYLE_COLOR_ICON)
->setHint($table, ZBX_STYLE_HINTBOX_WRAP_HORIZONTAL);
}
/**
* Create icon with hintbox for event messages.
*
* @param array $data
* int $data['count'] Number of messages.
* array $data['messages'] Array of messages.
* string $data['messages'][]['message'] Message text.
* string $data['messages'][]['clock'] Message creation time.
* @param array $users User name, surname and username.
*
* @throws Exception
*/
function makeEventMessagesIcon(array $data, array $users): ?CButtonIcon {
if ($data['count'] == 0) {
return null;
}
$table = (new CTableInfo())->setHeader([_('Time'), _('User'), _('Message')]);
for ($i = 0; $i < $data['count'] && $i < ZBX_WIDGET_ROWS; $i++) {
$message = $data['messages'][$i];
// Added in order to reuse makeActionTableUser().
$message['action_type'] = ZBX_EVENT_HISTORY_MANUAL_UPDATE;
$table->addRow([
zbx_date2str(DATE_TIME_FORMAT_SECONDS, $message['clock']),
makeActionTableUser($message, $users),
zbx_nl2br($message['message'])
]);
}
if ($data['count'] > ZBX_WIDGET_ROWS) {
$table->setFooter(
(new CCol(_s('Displaying %1$s of %2$s found', ZBX_WIDGET_ROWS, $data['count'])))
->addClass(ZBX_STYLE_LIST_TABLE_FOOTER)
);
}
return (new CButtonIcon(ZBX_ICON_ALERT_WITH_CONTENT))
->setAttribute('data-content', $data['count'])
->setAttribute('aria-label',
_xn('%1$s message', '%1$s messages', $data['count'], 'screen reader', $data['count'])
)
->setHint($table, ZBX_STYLE_HINTBOX_WRAP_HORIZONTAL);
}
/**
* Create icon with hintbox for event severity changes.
*
* @param array $data
* int $data['count'] Total number of severity changes.
* array $data['severities'] Array of severities.
* string $data['severities'][]['old_severity'] Event severity before change.
* string $data['severities'][]['new_severity'] Event severity after change.
* string $data['severities'][]['clock'] Severity change time.
* string $data['original_severity'] Severity before change.
* string $data['current_severity'] Current severity.
* @param array $users User name, surname and username.
*
* @throws Exception
*/
function makeEventSeverityChangesIcon(array $data, array $users): ?CButtonIcon {
if ($data['count'] == 0) {
return null;
}
$table = (new CTableInfo())->setHeader([_('Time'), _('User'), _('Severity changes')]);
for ($i = 0; $i < $data['count'] && $i < ZBX_WIDGET_ROWS; $i++) {
$severity = $data['severities'][$i];
// Added in order to reuse makeActionTableUser().
$severity['action_type'] = ZBX_EVENT_HISTORY_MANUAL_UPDATE;
// severity changes
$old_severity_name = CSeverityHelper::getName((int) $severity['old_severity']);
$new_severity_name = CSeverityHelper::getName((int) $severity['new_severity']);
$table->addRow([
zbx_date2str(DATE_TIME_FORMAT_SECONDS, $severity['clock']),
makeActionTableUser($severity, $users),
[$old_severity_name, NBSP(), RARR(), NBSP(), $new_severity_name]
]);
}
if ($data['count'] > ZBX_WIDGET_ROWS) {
$table->setFooter(
(new CCol(_s('Displaying %1$s of %2$s found', ZBX_WIDGET_ROWS, $data['count'])))
->addClass(ZBX_STYLE_LIST_TABLE_FOOTER)
);
}
if ($data['original_severity'] > $data['current_severity']) {
$button = (new CButtonIcon(ZBX_ICON_ARROW_DOWN_SMALL))
->addClass(ZBX_STYLE_COLOR_POSITIVE)
->setAttribute('aria-label', _x('Severity decreased', 'screen reader'));
}
elseif ($data['original_severity'] < $data['current_severity']) {
$button = (new CButtonIcon(ZBX_ICON_ARROW_UP_SMALL))
->addClass(ZBX_STYLE_COLOR_NEGATIVE)
->setAttribute('aria-label', _x('Severity increased', 'screen reader'));
}
else {
$button = (new CButtonIcon(ZBX_ICON_ARROWS_TOP_BOTTOM))
->addClass(ZBX_STYLE_COLOR_ICON)
->setAttribute('aria-label', _x('Severity changed', 'screen reader'));
}
return $button->setHint($table, ZBX_STYLE_HINTBOX_WRAP_HORIZONTAL);
}
/**
* @param array $actions Array with all actions sorted by clock.
* int $actions[]['action_type'] Type of action table entry (ZBX_EVENT_HISTORY_*).
* string $actions[]['clock'] Time, when action was performed.
* string $actions[]['message'] Message sent by alert, or written by manual update, or remote command text.
* string $actions[]['action'] Flag with problem update operation performed (only for ZBX_EVENT_HISTORY_MANUAL_UPDATE).
* string $actions[]['alerttype'] Type of alert (only for ZBX_EVENT_HISTORY_ALERT).
* string $actions[]['mediatypeid'] Id for mediatype, where alert message was sent (only for ZBX_EVENT_HISTORY_ALERT).
* string $actions[]['error'] Error message in case of failed alert (only for ZBX_EVENT_HISTORY_ALERT).
* @param array $users User name, surname and username.
* @param array $mediatypes Mediatypes with maxattempts value and name.
* string $mediatypes[]['name'] Mediatype name.
* string $mediatypes[]['maxattempts'] Maximum attempts for this mediatype.
*
* @throws Exception
*/
function makeEventActionsTable(array $actions, array $users, array $mediatypes): CTableInfo {
$action_count = count($actions);
$table = (new CTableInfo())->setHeader([
_('Time'), _('User/Recipient'), _('Action'), _('Message/Command'), _('Status'), _('Info')
]);
for ($i = 0; $i < $action_count && $i < ZBX_WIDGET_ROWS; $i++) {
$action = $actions[$i];
$message = '';
if ($action['action_type'] == ZBX_EVENT_HISTORY_MANUAL_UPDATE
&& ($action['action'] & ZBX_PROBLEM_UPDATE_MESSAGE) == ZBX_PROBLEM_UPDATE_MESSAGE) {
$message = zbx_nl2br($action['message']);
}
elseif ($action['action_type'] == ZBX_EVENT_HISTORY_ALERT) {
if ($action['alerttype'] == ALERT_TYPE_COMMAND) {
$message = _('Remote command');
}
elseif ($action['alerttype'] == ALERT_TYPE_MESSAGE) {
$message = array_key_exists($action['mediatypeid'], $mediatypes)
? $mediatypes[$action['mediatypeid']]['name']
: '';
}
}
$table->addRow([
zbx_date2str(DATE_TIME_FORMAT_SECONDS, $action['clock']),
makeActionTableUser($action, $users),
makeActionTableIcon($action),
$message,
makeActionTableStatus($action),
makeActionTableInfo($action, $mediatypes)
]);
}
return $table;
}
/**
* Create icon with hintbox for event actions.
*
* @param array $data
* int $data['count'] Number of actions.
* bool $data['has_uncomplete_action'] Does the event have at least one uncompleted alert action.
* bool $data['has_failed_action'] Does the event have at least one failed alert action.
* @param string $eventid
*/
function makeEventActionsIcon(array $data, $eventid): ?CButtonIcon {
if ($data['count'] == 0) {
return null;
}
$button = new CButtonIcon(ZBX_ICON_BULLET_RIGHT_WITH_CONTENT);
if ($data['has_failed_action']) {
$button->addClass(ZBX_STYLE_COLOR_NEGATIVE);
}
elseif ($data['has_uncomplete_action']) {
$button->addClass(ZBX_STYLE_COLOR_WARNING);
}
return $button
->setAttribute('data-content', $data['count'])
->setAttribute('aria-label',
_xn('%1$s action', '%1$s actions', $data['count'], 'screen reader', $data['count'])
)
->setAjaxHint([
'type' => 'eventactions',
'data' => ['eventid' => $eventid]
], ZBX_STYLE_HINTBOX_WRAP_HORIZONTAL);
}
/**
* Get table with list of event actions for event details page.
*
* @param array $data
* array $data['actions'] Array with all actions sorted by clock.
* int $data['actions'][]['action_type'] Type of action table entry (ZBX_EVENT_HISTORY_*).
* string $data['actions'][]['clock'] Time, when action was performed.
* string $data['actions'][]['message'] Message sent by alert, or written by manual update, or remote command text.
* string $data['actions'][]['alerttype'] Type of alert (only for ZBX_EVENT_HISTORY_ALERT).
* string $data['actions'][]['esc_step'] Alert escalation step (only for ZBX_EVENT_HISTORY_ALERT).
* string $data['actions'][]['subject'] Message alert subject (only for ZBX_EVENT_HISTORY_ALERT).
* string $data['actions'][]['p_eventid'] Problem eventid that was reason for alert (only for ZBX_EVENT_HISTORY_ALERT).
* string $data['actions'][]['acknowledgeid'] Problem update action that was reason for alert (only for ZBX_EVENT_HISTORY_ALERT).
* @param array $users User name, surname and username.
* @param array $mediatypes Mediatypes with maxattempts value.
*/
function makeEventDetailsActionsTable(array $data, array $users, array $mediatypes): CTableInfo {
$table = (new CTableInfo())->setHeader([
_('Step'), _('Time'), _('User/Recipient'), _('Action'), _('Message/Command'), _('Status'), _('Info')
]);
foreach ($data['actions'] as $action) {
$esc_step = '';
if ($action['action_type'] == ZBX_EVENT_HISTORY_ALERT && $action['p_eventid'] == 0
&& $action['acknowledgeid'] == 0) {
/*
* Escalation step should be displayed, only if alert is caused by problem event.
* Escalation step should not be displayed, if alert is caused by resolve event, or by problem update.
*/
$esc_step = $action['esc_step'];
}
$message = '';
switch ($action['action_type']) {
case ZBX_EVENT_HISTORY_ALERT:
switch ($action['alerttype']) {
case ALERT_TYPE_MESSAGE:
$message = [bold($action['subject']), BR(), BR(), zbx_nl2br($action['message'])];
break;
case ALERT_TYPE_COMMAND:
$message = [bold(_('Command').':'), BR(), zbx_nl2br($action['message'])];
break;
}
break;
case ZBX_EVENT_HISTORY_MANUAL_UPDATE:
$message = zbx_nl2br($action['message']);
break;
}
$table->addRow([
$esc_step,
zbx_date2str(DATE_TIME_FORMAT_SECONDS, $action['clock']),
makeEventDetailsTableUser($action, $users),
makeActionTableIcon($action),
$message,
makeActionTableStatus($action),
makeActionTableInfo($action, $mediatypes)
]);
}
return $table;
}
/**
* Get table with list of event updates for update event page.
*
* @param array $actions Array with all actions sorted by clock.
* string $actions[]['clock'] Time, when action was performed.
* string $actions[]['message'] Message sent by alert, or written by manual update, or remote command text.
* @param array $users User name, surname and username.
*/
function makeEventHistoryTable(array $actions, array $users): CTable {
$table = (new CTable())
->addStyle('width: 100%;')
->setHeader([_('Time'), _('User'), _('User action'), _('Message')]);
foreach ($actions as $action) {
// Added in order to reuse makeActionTableUser() and makeActionTableIcon()
$action['action_type'] = ZBX_EVENT_HISTORY_MANUAL_UPDATE;
$table->addRow([
zbx_date2str(DATE_TIME_FORMAT_SECONDS, $action['clock']),
makeActionTableUser($action, $users),
makeActionTableIcon($action),
(new CCol(zbx_nl2br($action['message'])))->addClass(ZBX_STYLE_TABLE_FORMS_OVERFLOW_BREAK)
]);
}
return $table;
}
/**
* Creates username for message author or alert receiver.
*
* @param array $action Array of action data.
* int $action['action_type'] Type of event table action (ZBX_EVENT_HISTORY_*).
* string $action['alerttype'] Type of alert.
* string $action['userid'] ID of message author, or alert receiver.
* @param array $users Array with user data - username, name, surname.
*/
function makeActionTableUser(array $action, array $users): string {
if (($action['action_type'] == ZBX_EVENT_HISTORY_ALERT && $action['alerttype'] == ALERT_TYPE_MESSAGE)
|| $action['action_type'] == ZBX_EVENT_HISTORY_MANUAL_UPDATE) {
return array_key_exists($action['userid'], $users)
? getUserFullname($users[$action['userid']])
: _('Inaccessible user');
}
else {
return '';
}
}
/**
* Creates username for message author or alert receiver. Also contains 'sendto' for message actions.
*
* @param array $action
* int $action['action_type'] Type of event table action (ZBX_EVENT_HISTORY_*).
* string $action['alerttype'] Type of alert.
* array $action['userid'] ID of message author, or alert receiver.
* array $action['sendto'] Receiver media address for automatic action.
* @param array $users Array with user data - username, name, surname.
*
* @return array|string
*/
function makeEventDetailsTableUser(array $action, array $users) {
if ($action['action_type'] == ZBX_EVENT_HISTORY_ALERT && $action['alerttype'] == ALERT_TYPE_MESSAGE) {
return array_key_exists($action['userid'], $users)
? [getUserFullname($users[$action['userid']]), BR(), italic(zbx_nl2br($action['sendto']))]
: _('Inaccessible user');
}
elseif ($action['action_type'] == ZBX_EVENT_HISTORY_MANUAL_UPDATE) {
return array_key_exists($action['userid'], $users)
? getUserFullname($users[$action['userid']])
: _('Inaccessible user');
}
else {
return '';
}
}
/**
* Make appropriate icon for event action.
*
* @param array $action Array with actions table data.
* int $action['action_type'] Type of action table entry (ZBX_EVENT_HISTORY_*).
* int $action['action'] Flag with problem update operation performed (only for ZBX_EVENT_HISTORY_MANUAL_UPDATE).
* int $action['old_severity'] Severity before problem update (only for ZBX_EVENT_HISTORY_MANUAL_UPDATE).
* int $action['new_severity'] Severity after problem update (only for ZBX_EVENT_HISTORY_MANUAL_UPDATE).
* int $action['alerttype'] Type of alert (only for ZBX_EVENT_HISTORY_ALERT).
*/
function makeActionTableIcon(array $action): ?CTag {
switch ($action['action_type']) {
case ZBX_EVENT_HISTORY_PROBLEM_EVENT:
return new CIcon(ZBX_ICON_CALENDAR_WARNING, _('Problem created'));
case ZBX_EVENT_HISTORY_RECOVERY_EVENT:
return new CIcon(ZBX_ICON_CALENDAR_CHECK, _('Problem resolved'));
case ZBX_EVENT_HISTORY_MANUAL_UPDATE:
$action_icons = [];
if (($action['action'] & ZBX_PROBLEM_UPDATE_CLOSE) == ZBX_PROBLEM_UPDATE_CLOSE) {
$action_icons[] = new CIcon(ZBX_ICON_CHECKBOX, _('Manually closed'));
}
if (($action['action'] & ZBX_PROBLEM_UPDATE_RANK_TO_CAUSE) == ZBX_PROBLEM_UPDATE_RANK_TO_CAUSE) {
$action_icons[] = new CIcon(ZBX_ICON_ARROW_RIGHT_TOP, _('Cause'));
}
if (($action['action'] & ZBX_PROBLEM_UPDATE_RANK_TO_SYMPTOM) == ZBX_PROBLEM_UPDATE_RANK_TO_SYMPTOM) {
$action_icons[] = new CIcon(ZBX_ICON_ARROW_TOP_RIGHT, _('Symptom'));
}
if (($action['action'] & ZBX_PROBLEM_UPDATE_ACKNOWLEDGE) == ZBX_PROBLEM_UPDATE_ACKNOWLEDGE) {
$action_icons[] = new CIcon(ZBX_ICON_CHECK, _('Acknowledged'));
}
if (($action['action'] & ZBX_PROBLEM_UPDATE_UNACKNOWLEDGE) == ZBX_PROBLEM_UPDATE_UNACKNOWLEDGE) {
$action_icons[] = new CIcon(ZBX_ICON_UNCHECK, _('Unacknowledged'));
}
if (($action['action'] & ZBX_PROBLEM_UPDATE_SUPPRESS) == ZBX_PROBLEM_UPDATE_SUPPRESS) {
if ($action['suppress_until'] == ZBX_PROBLEM_SUPPRESS_TIME_INDEFINITE) {
$suppress_until = _s('Indefinitely');
}
else {
$suppress_until = $action['suppress_until'] < strtotime('tomorrow')
&& $action['suppress_until'] > strtotime('today')
? zbx_date2str(TIME_FORMAT, $action['suppress_until'])
: zbx_date2str(DATE_TIME_FORMAT, $action['suppress_until']);
}
$action_icons[] = (new CButtonIcon(ZBX_ICON_EYE_OFF))
->addClass(ZBX_STYLE_COLOR_ICON)
->setHint(_s('Suppressed till: %1$s', $suppress_until), ZBX_STYLE_HINTBOX_WRAP_HORIZONTAL);
}
if (($action['action'] & ZBX_PROBLEM_UPDATE_UNSUPPRESS) == ZBX_PROBLEM_UPDATE_UNSUPPRESS) {
$action_icons[] = new CIcon(ZBX_ICON_EYE, _('Unsuppressed'));
}
if (($action['action'] & ZBX_PROBLEM_UPDATE_MESSAGE) == ZBX_PROBLEM_UPDATE_MESSAGE) {
$action_icons[] = new CIcon(ZBX_ICON_ALERT_MORE, _('Message'));
}
if (($action['action'] & ZBX_PROBLEM_UPDATE_SEVERITY) == ZBX_PROBLEM_UPDATE_SEVERITY) {
$button = $action['new_severity'] > $action['old_severity']
? (new CButtonIcon(ZBX_ICON_ARROW_UP_SMALL))->addClass(ZBX_STYLE_COLOR_NEGATIVE)
: (new CButtonIcon(ZBX_ICON_ARROW_DOWN_SMALL))->addClass(ZBX_STYLE_COLOR_POSITIVE);
$old_severity_name = CSeverityHelper::getName((int) $action['old_severity']);
$new_severity_name = CSeverityHelper::getName((int) $action['new_severity']);
$action_icons[] = $button->setHint(
[$old_severity_name, NBSP(), RARR(), NBSP(), $new_severity_name], ZBX_STYLE_HINTBOX_WRAP_HORIZONTAL
);
}
return (new CCol($action_icons))->addClass(ZBX_STYLE_NOWRAP);
case ZBX_EVENT_HISTORY_ALERT:
return $action['alerttype'] == ALERT_TYPE_COMMAND
? new CIcon(ZBX_ICON_COMMAND, _('Remote command'))
: new CIcon(ZBX_ICON_ENVELOPE_FILLED, _('Alert message'));
default:
return null;
}
}
/**
* Creates span with alert status text.
*
* @param array $action
* int $action['action_type'] Type of event table action (ZBX_EVENT_HISTORY_*).
* string $action['status'] Alert status.
* string $action['alerttype'] Type of alert.
*
* @return CSpan|string
*/
function makeActionTableStatus(array $action) {
if ($action['action_type'] != ZBX_EVENT_HISTORY_ALERT) {
return '';
}
switch ($action['status']) {
case ALERT_STATUS_SENT:
$status_label = ($action['alerttype'] == ALERT_TYPE_COMMAND)
? _('Executed')
: _('Sent');
$status_color = ZBX_STYLE_GREEN;
break;
case ALERT_STATUS_NEW:
case ALERT_STATUS_NOT_SENT:
$status_label = _('In progress');
$status_color = ZBX_STYLE_YELLOW;
break;
default:
$status_label = _('Failed');
$status_color = ZBX_STYLE_RED;
break;
}
return (new CSpan($status_label))->addClass($status_color);
}
/**
* Creates div with alert info icons.
*
* @param array $action
* int $action['action_type'] Type of event table action (ZBX_EVENT_HISTORY_*).
* string $action['status'] Alert status.
* string $action['alerttype'] Type of alert.
* string $action['mediatypeid'] ID for mediatype, where alert message was sent.
* string $action['retries'] How many retries were done for pending alert message.
* @param array $mediatypes Array of media type data.
* array $mediatypes[]['maxattempts'] Maximum attempts for this mediatype.
*
* @return CDiv|string
*/
function makeActionTableInfo(array $action, array $mediatypes) {
if ($action['action_type'] == ZBX_EVENT_HISTORY_ALERT) {
$info_icons = [];
if ($action['alerttype'] == ALERT_TYPE_MESSAGE
&& ($action['status'] == ALERT_STATUS_NEW || $action['status'] == ALERT_STATUS_NOT_SENT)) {
$info_icons[] = makeWarningIcon(array_key_exists($action['mediatypeid'], $mediatypes)
? _n('%1$s retry left', '%1$s retries left',
$mediatypes[$action['mediatypeid']]['maxattempts'] - $action['retries']
)
: ''
);
}
elseif ($action['error'] !== '') {
$info_icons[] = makeErrorIcon($action['error']);
}
return makeInformationList($info_icons);
}
else {
return '';
}
}
|