Actions are responsible for 'business logic' of the module. An action usually consists of a controller and an action view.
A module can:
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.
Action controller workflow:
Check user permissions.
Prepare the data according to passed parameters: if checkInput() returns true, Zabbix calls the controller's doAction() method.
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.