Table of Contents

Actions

Actions are responsible for 'business logic' of the module. An action usually consists of a controller and an action view.

A module can:

  • Call actions that are already defined in Zabbix frontend.
  • Override default actions with custom actions.
  • Define completely new actions.

To override a default action behavior with some custom behavior, define an action with the same name in the module configuration. When the action is called, the module action will be executed instead of the default Zabbix action.

Action files should be stored in the actions folder. The actions need to be specified in the manifest.json.

Controller

Action controller workflow:

  1. Check that all parameters passed in an HTTP request are valid:
  • Call the controller's checkInput() method
  • Use validation rules defined in CNewValidator.php
  • Call validateInput() method
  1. Check user permissions.

  2. Prepare the data according to passed parameters: if checkInput() returns true, Zabbix calls the controller's doAction() method.

  3. Prepare the $data array for the view. Use CControllerResponseData and setResponse() method to store response in the $data array.

Example:

/**
        * Validate input parameters.
        *
        * @return bool
        */
       protected function checkInput(): bool {
           $ret = $this->validateInput([
               'status' => 'in '.implode(',', [HOST_STATUS_MONITORED, HOST_STATUS_NOT_MONITORED])
           ]);
       
           if (!$ret) {
               $this->setResponse(new CControllerResponseFatal());
           }
       
           return $ret;
       }
       
       /**
        * Check user permissions.
        *
        * @return bool
        */
       protected function checkPermissions() {
           return $this->getUserType() >= USER_TYPE_ZABBIX_ADMIN;
       }
       
       /**
        * Execute action and generate response object.
        */
       protected function do Action(): void {
           $data = [ 
               'hosts_count' => API::Host()->get([
                   'countOutput' => true,
                   'filter' => [
                       'status' => $this->getInput('status')
                   ]
               ]) 
           ];
           
           $this->setResponse(new CControllerResponseData($data));
       }

You can view the full list of available controller classes in Zabbix source code.