zabbix remote command configuration

Keywords: Zabbix Nginx sudo ssh

1. zabbix remote command:

1.1 Function:

    Restart service;

    Restart the server through IPMI interface.

    Functions that any custom script can accomplish: cleaning disk space, virtual machine instance migration, etc.

1.2 Related attributes:

    Tatget list: The target host for remote command execution, which can authenticate the current host, other host, or host group

    Type: Command Type

        IPMI:IPMI command;

        Custom script: Custom script, which can be executed on zabbix server or zabbix agent.

        SSH: To execute commands through ssh, it is necessary to provide user accounts on the target host, relevant authentication methods and additional information needed for authentication.

        Telnet: To execute commands through Telnet, you need to specify a user name, password, and port for remote host telnet services to listen on.

        Global script: A global script that executes one of the scripts defined by "Administration - > Scripts";

    Commands: Commands to be executed 

2. Prerequisites for use

Configuration that needs to be done in the agent:

(1)zabbix users have the rights to be managed (based on sudo authorization)

~]# visudo
# %wheel        ALL=(ALL)       NOPASSWD: ALL
zabbix  ALL=(ALL)       NOPASSWD: ALL
# Defaults    requiretty# Comment on the line

Description of this configuration:

     1) Defaults requiretty, modified to # Defaults requiretty, indicating that no control terminal is required (commands can be executed without login from the terminal)

     # su - zabbix -c 'sudo /usr/bin/systemctl restart httpd.service'

     2) Defaults requiretty, modified to Defaults: nobody! Requiretty, indicating that only nobody users do not need to control the terminal (only nobody users do not need to login from the terminal to execute commands)

(2) The agent process should allow remote commands to be executed;

~]# vim /etc/zabbix/zabbix_agentd.conf
EnableRemoteCommands=1# Open remote commands
### Option: LogRemoteCommands
#       Enable logging of executed shell commands as warnings.
#       0 - disabled
#       1 - enabled
#
# Mandatory: no
# Default:
# LogRemoteCommands=0
LogRemoteCommands=1# Logging

(3) Restart services

~]# systemctl restart zabbix-agent.service

(4)zabbix user login

~]# mkdir /var/lib/zabbix# Create a home directory, don't do it
~]# grep "^zabbix" /etc/passwd
zabbix:x:996:994:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
~]# usermod -s /bin/bash zabbix
~]# grep "^zabbix" /etc/passwd
zabbix:x:996:994:Zabbix Monitoring System:/var/lib/zabbix:/bin/bash

3. Detect the 80 port of tcp. If the 80 port of TCP is not monitored, the nginx server will be restarted automatically.

Install nginx on the monitored host

~]# yum install -y nginx
~]# systemctl restart nginx.service    # Start up service

Configure item and trigger on zabbix web to collect monitoring data and set triggers

item:

trigger:

Action configuration, focusing on the configuration of Operation details:


Verification:

Stop nginx service

~#] su - zabbix -c "sudo /usr/bin/systemctl stop  nginx.service"
# This method can verify whether zabbix can execute commands without login terminal.
//There will be the following errors, do not affect the use, do not pay attention to:
Error creating textual authentication agent: Error opening current controlling terminal for the process (`/dev/tty'): No such device or address (polkit-error-quark, 0)

Triggers in Montoring can see triggered monitors.

In Events, you can see the triggered event, Action already prompted:

Operational logs can be viewed in the / var/log/zabbix/zabbix_agent D.Log directory on agent host (error causes can be found through logs):

  4620:20170511:111934.635 Executing command '/usr/bin/systemctl restart nginx.service'

It was found that port 80 had been re-monitored on agent host:

[root@wlm ~]# ss -tnl | grep 80
LISTEN     0      128          *:80                       *:*                  
LISTEN     0      128         :::80                      :::*

The alarm on zabbix web has also been lifted.

Posted by 7khat on Mon, 01 Jul 2019 13:29:35 -0700