| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php declare(strict_types = 0);
- /*
- ** WeCom Center - GET controller. Gathers current state and renders the page.
- */
- namespace Modules\WecomCenter\Actions;
- use CController;
- use CControllerResponseData;
- use CWebUser;
- use Modules\WecomCenter\Lib\Helper;
- use Modules\WecomCenter\Lib\I18n;
- class WecomCenterView extends CController {
- protected function init() {
- $this->disableCsrfValidation();
- }
- protected function checkInput() {
- return true;
- }
- protected function checkPermissions() {
- return CWebUser::getType() == USER_TYPE_SUPER_ADMIN;
- }
- protected function doAction() {
- $module_dir = dirname(__DIR__);
- $mediatypes = Helper::webhookMediatypes();
- $mediatypeid = (string) ($_REQUEST['mediatypeid'] ?? '');
- if ($mediatypeid === '' && $mediatypes) {
- foreach ($mediatypes as $mt) {
- if (stripos($mt['name'], 'wecom') !== false) {
- $mediatypeid = (string) $mt['mediatypeid'];
- break;
- }
- }
- if ($mediatypeid === '') {
- $mediatypeid = (string) $mediatypes[0]['mediatypeid'];
- }
- }
- $mediatype = null;
- $webhook_url = '';
- $media = null;
- if ($mediatypeid !== '') {
- $mediatype = Helper::getMediatype($mediatypeid);
- if ($mediatype) {
- $webhook_url = Helper::webhookKey($mediatype);
- }
- $robot = Helper::robotUser('wecomrobot');
- $media = Helper::currentMedia($robot, $mediatypeid);
- }
- // ---- alert strategy ----
- $template_hostid = Helper::templateHostid();
- $current_strategy = Helper::currentStrategy();
- $preview = null;
- $preview_key = (string) ($_REQUEST['preview_preset'] ?? '');
- if ($preview_key !== '' && isset(Helper::STRATEGY_PRESETS[$preview_key]) && $template_hostid !== null) {
- $preview = Helper::previewStrategy($template_hostid,
- Helper::STRATEGY_PRESETS[$preview_key]['targets']);
- }
- // ---- server tuning generator ----
- $tuning_profile = (string) ($_REQUEST['tuning_profile'] ?? 'medium');
- if (!isset(Helper::TUNING_PROFILES[$tuning_profile])) {
- $tuning_profile = 'medium';
- }
- $tuning = Helper::tuningSnippet($tuning_profile);
- // ---- map: single marker = Zabbix server host ----
- $server_host = null; $server_loc = ['location_lat' => '', 'location_lon' => ''];
- try {
- $sh = \API::Host()->get([
- 'output' => ['hostid', 'name'],
- 'filter' => ['host' => 'Zabbix server'],
- 'selectInventory' => ['location_lat', 'location_lon'],
- 'limit' => 1
- ]);
- if ($sh) {
- $server_host = $sh[0];
- $server_loc = [
- 'location_lat' => $sh[0]['inventory']['location_lat'] ?? '',
- 'location_lon' => $sh[0]['inventory']['location_lon'] ?? ''
- ];
- }
- }
- catch (\Throwable $e) { /* ignore */ }
- // ---- maintenance: per-trigger alerting switch ----
- $trig_search = trim((string) ($_REQUEST['trig_search'] ?? ''));
- $triggers = Helper::triggers($trig_search);
- $trigger_count = Helper::triggerCount();
- // ---- template switch / migration ----
- $huawei_tpls = Helper::huaweiTemplates();
- $switch_src = (string) ($_REQUEST['switch_src'] ?? '');
- $switch_dst = (string) ($_REQUEST['switch_dst'] ?? '');
- $switch_preview = null;
- if ($switch_src !== '' && $switch_dst !== '' && $switch_src !== $switch_dst) {
- $switch_preview = Helper::switchPreview($switch_src, $switch_dst);
- }
- $data = [
- 'title' => I18n::t('page.title'),
- 'mediatypes' => $mediatypes,
- 'mediatypeid' => $mediatypeid,
- 'mediatype' => $mediatype,
- 'webhook_url' => $webhook_url,
- 'media' => $media,
- 'severity' => $media['severity'] ?? 48,
- 'active' => $media['active'] ?? 0,
- 'presets' => Helper::STRATEGY_PRESETS,
- 'classes' => Helper::CLASSES,
- 'current_strategy'=> $current_strategy,
- 'template_hostid' => $template_hostid,
- 'preview_key' => $preview_key,
- 'preview' => $preview,
- 'tuning_profile' => $tuning_profile,
- 'tuning' => $tuning,
- 'tuning_profiles' => Helper::TUNING_PROFILES,
- 'templates' => Helper::bundledTemplates($module_dir),
- 'server_host' => $server_host,
- 'server_loc' => $server_loc,
- 'trig_search' => $trig_search,
- 'triggers' => $triggers,
- 'trigger_count' => $trigger_count,
- 'huawei_tpls' => $huawei_tpls,
- 'switch_src' => $switch_src,
- 'switch_dst' => $switch_dst,
- 'switch_preview' => $switch_preview,
- 'lang' => I18n::lang()
- ];
- $response = new CControllerResponseData($data);
- $response->setTitle(I18n::t('page.title'));
- $this->setResponse($response);
- }
- }
|