WecomCenterView.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php declare(strict_types = 0);
  2. /*
  3. ** WeCom Center - GET controller. Gathers current state and renders the page.
  4. */
  5. namespace Modules\WecomCenter\Actions;
  6. use CController;
  7. use CControllerResponseData;
  8. use CWebUser;
  9. use Modules\WecomCenter\Lib\Helper;
  10. use Modules\WecomCenter\Lib\I18n;
  11. use Modules\WecomCenter\Lib\L2Store;
  12. class WecomCenterView extends CController {
  13. protected function init() {
  14. $this->disableCsrfValidation();
  15. }
  16. protected function checkInput() {
  17. return true;
  18. }
  19. protected function checkPermissions() {
  20. return CWebUser::getType() == USER_TYPE_SUPER_ADMIN;
  21. }
  22. protected function doAction() {
  23. $module_dir = dirname(__DIR__);
  24. $mediatypes = Helper::webhookMediatypes();
  25. $mediatypeid = (string) ($_REQUEST['mediatypeid'] ?? '');
  26. if ($mediatypeid === '' && $mediatypes) {
  27. foreach ($mediatypes as $mt) {
  28. if (stripos($mt['name'], 'wecom') !== false) {
  29. $mediatypeid = (string) $mt['mediatypeid'];
  30. break;
  31. }
  32. }
  33. if ($mediatypeid === '') {
  34. $mediatypeid = (string) $mediatypes[0]['mediatypeid'];
  35. }
  36. }
  37. $mediatype = null;
  38. $webhook_url = '';
  39. $media = null;
  40. if ($mediatypeid !== '') {
  41. $mediatype = Helper::getMediatype($mediatypeid);
  42. if ($mediatype) {
  43. $webhook_url = Helper::webhookKey($mediatype);
  44. }
  45. $robot = Helper::robotUser('wecomrobot');
  46. $media = Helper::currentMedia($robot, $mediatypeid);
  47. }
  48. // ---- alert strategy ----
  49. $template_hostid = Helper::templateHostid();
  50. $current_strategy = Helper::currentStrategy();
  51. $preview = null;
  52. $preview_key = (string) ($_REQUEST['preview_preset'] ?? '');
  53. if ($preview_key !== '' && isset(Helper::STRATEGY_PRESETS[$preview_key]) && $template_hostid !== null) {
  54. $preview = Helper::previewStrategy($template_hostid,
  55. Helper::STRATEGY_PRESETS[$preview_key]['targets']);
  56. }
  57. // ---- server tuning generator ----
  58. $tuning_profile = (string) ($_REQUEST['tuning_profile'] ?? 'medium');
  59. if (!isset(Helper::TUNING_PROFILES[$tuning_profile])) {
  60. $tuning_profile = 'medium';
  61. }
  62. $tuning = Helper::tuningSnippet($tuning_profile);
  63. // ---- map: single marker = Zabbix server host ----
  64. $server_host = null; $server_loc = ['location_lat' => '', 'location_lon' => ''];
  65. try {
  66. $sh = \API::Host()->get([
  67. 'output' => ['hostid', 'name'],
  68. 'filter' => ['host' => 'Zabbix server'],
  69. 'selectInventory' => ['location_lat', 'location_lon'],
  70. 'limit' => 1
  71. ]);
  72. if ($sh) {
  73. $server_host = $sh[0];
  74. $server_loc = [
  75. 'location_lat' => $sh[0]['inventory']['location_lat'] ?? '',
  76. 'location_lon' => $sh[0]['inventory']['location_lon'] ?? ''
  77. ];
  78. }
  79. }
  80. catch (\Throwable $e) { /* ignore */ }
  81. // ---- maintenance: per-trigger alerting switch ----
  82. $trig_search = trim((string) ($_REQUEST['trig_search'] ?? ''));
  83. $triggers = Helper::triggers($trig_search);
  84. $trigger_count = Helper::triggerCount();
  85. // ---- template switch / migration ----
  86. $huawei_tpls = Helper::huaweiTemplates();
  87. $switch_src = (string) ($_REQUEST['switch_src'] ?? '');
  88. $switch_dst = (string) ($_REQUEST['switch_dst'] ?? '');
  89. $switch_preview = null;
  90. if ($switch_src !== '' && $switch_dst !== '' && $switch_src !== $switch_dst) {
  91. $switch_preview = Helper::switchPreview($switch_src, $switch_dst);
  92. }
  93. // ---- L2 topology configuration ----
  94. L2Store::ensureSchema();
  95. $l2 = L2Store::getConfig();
  96. $l2_templates = [];
  97. try {
  98. $tpls = \API::Template()->get(['output' => ['templateid', 'name'], 'sortfield' => 'name']);
  99. foreach ($tpls as $tp) {
  100. $l2_templates[] = $tp['name'];
  101. }
  102. }
  103. catch (\Throwable $e) { /* ignore */ }
  104. $data = [
  105. 'title' => I18n::t('page.title'),
  106. 'l2' => $l2,
  107. 'l2_templates' => $l2_templates,
  108. 'mediatypes' => $mediatypes,
  109. 'mediatypeid' => $mediatypeid,
  110. 'mediatype' => $mediatype,
  111. 'webhook_url' => $webhook_url,
  112. 'media' => $media,
  113. 'severity' => $media['severity'] ?? 48,
  114. 'active' => $media['active'] ?? 0,
  115. 'presets' => Helper::STRATEGY_PRESETS,
  116. 'classes' => Helper::CLASSES,
  117. 'current_strategy'=> $current_strategy,
  118. 'template_hostid' => $template_hostid,
  119. 'preview_key' => $preview_key,
  120. 'preview' => $preview,
  121. 'tuning_profile' => $tuning_profile,
  122. 'tuning' => $tuning,
  123. 'tuning_profiles' => Helper::TUNING_PROFILES,
  124. 'templates' => Helper::bundledTemplates($module_dir),
  125. 'server_host' => $server_host,
  126. 'server_loc' => $server_loc,
  127. 'trig_search' => $trig_search,
  128. 'triggers' => $triggers,
  129. 'trigger_count' => $trigger_count,
  130. 'huawei_tpls' => $huawei_tpls,
  131. 'switch_src' => $switch_src,
  132. 'switch_dst' => $switch_dst,
  133. 'switch_preview' => $switch_preview,
  134. 'lang' => I18n::lang()
  135. ];
  136. $response = new CControllerResponseData($data);
  137. $response->setTitle(I18n::t('page.title'));
  138. $this->setResponse($response);
  139. }
  140. }