WecomCenterView.php 4.4 KB

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