This article includes: four lines of shell script to implement ZABBIX_ High availability of server, ZABBIX_ How to solve the Chinese garbled code when the server executes the window script.
Transferred from @ TWT community, [author] Liu Junyuan
Four line shell script to realize ZABBIX_ High availability of server
The importance of high availability
A small and medium-sized enterprise needs to use ZABBIX_ Server monitors a large number of devices. One day, a new employee made a mistake in operating the server, which led to ZABBIX_ The server system broke down and could not be started. This is ZABBIX_ If the server service is interrupted, all the devices of this enterprise cannot be monitored. If there is an important business host in the monitored device at this time, an alarm will appear. However, due to ZABBIX_ The server is unable to collect data, so the operation and maintenance personnel do not know that there is a serious alarm on the business host, and the business is also affected. Finally, it brings huge economic losses to enterprises.
What is high availability?
High availability technology is simply to ensure that the business in the event of failure, fast switching, so as not to affect the normal operation of the business.
What is VIP (virtual IP)?
High availability technology cannot do without VIP. The principle of high availability is that users access services through VIP, and VIP will map to the corresponding server IP.
By default, the client maps to the main ZABBIX 192.168.74.149 through the IP of 192.168.74.150_ Server, if the main ZABBIX_ If the server is shut down, the client will continue to use 192.168.74.150 IP, which will be mapped to the standby ZABBIX_ Server (192.168.74.148).
Install two ZABBIX_ Server, a database server (main ZABBIX)_ Server and standby ZABBIX_ The data of server is saved in this database.)
Prepared by: ZABBIX_ server:192.168.74.148
Main: ZABBIX server: 192.168.74.149
One database server: 192.168.74.128
The content of this experiment needs to use logical network card as VIP (192.168.74.150)
Step one: on both ZABBIX_ Create a logical network card on the server, VIP
Two ZABBIX_ The server enters the / etc / sysconfig / network script directory
C d /etc /sysconfig/network-scripts
Copy the current server network card file, my network card is ifcfg – ens33.
Cp ifcfg-ens33 ifcfg-ens33:0
Edit ifcfg – ens33:0 file (two ZABBIX)_ In the ifcfg-ens33:0 file of server, IP is changed to 192.168.74.150 as virtual IP
Now any ZABBIX_ Start ifcfg – ens33:0 on the server to confirm whether the network card can be started.
Note that you can’t use two ZABBIX at the same time_ Server enables ifcfg – ens33:0, otherwise IP conflicts will occur.
The logical network card startup command is if up network card name. If you use the ifup command, you can only restart the new network card. It is better not to restart all network cards with the system CTL network restart command. Once this command causes the network card startup failure, it will lead to the failure of remote server.
If up ifcfg -ens33:0
Step 2: verify whether the main ZABBIX can be accessed through VIP (192.168.74.150)_ Server or ZABBIX_ IP address of server
Install the agent on the window host and change the agent configuration file to server_ IP points to the virtual IP 192.168.74.150. Confirm whether it can be mapped to the ZABBIX 192.168.74.149 through 192.168.74.150_ Server service
At this time, ZABBIX of 192.168.74.149 can be accessed through virtual IP (192.168.74.150)_ server 。
But at the same time, there is a problem, as shown in the figure, all the monitoring host ZBx appears red
In order to solve the above problem, it is necessary to modify the server IP of ZABBIX agent.conf of all monitoring hosts to point to 192.168.74.150
After modifying the agent configuration, ZABBIX must be restarted_ agent
The most important step is to modify the two ZABBIX_ The parameter source in server. Conf is specified as virtual IP
192.168.74.150, otherwise the ZBx of the interface will be red
If the source IP is not modified, the following problems still cannot be solved
Modify ZABBIX_ Server.conf file, source IP changed to virtual IP, 192.168.74.150
Restart ZABBIX_ server
Systemctl restart zabbix_server
After the above configuration is completed, the monitoring host of the interface returns to normal
The data of the host is also normal
Now start to write shell script to realize the main ZABBIX_ After the server (192.168.74.149) is down, you can still access the standby ZABBIX through 192.168.74.150_ server （192.168.74.148）
Write script check on 192.168.74.149 and 192.168.74.148 servers_ alive
These two scripts are added to the timing task of crontab – E on both servers. The time to execute these two scripts is user-defined. I temporarily run this test script in 60s. Remember to give these two scripts executable permissions
The reason why the script is set to be executed once every 60s is to shut down the virtual machine 192.168.74.149 after the demonstration, and then the interface will be inaccessible. Then wait for 60 seconds, spare ZABBIX_ Server 192.168.74.148 will enable VIP, and the interface can continue to access ZABBIX_ server 。 Normally, it is more appropriate to set it to 3-10s, so as to achieve fast switching
C hmo d o+x 755 check_alive.sh
Remember to use the absolute path, otherwise you may report an error
Final effect demonstration
Turn off main ZABBIX_ Server (192.168.74.149 virtual machine)
At this time, the VIP is on the main ZABBIX server
Turn off main ZABBIX_ Server (192.168.74.149), ZABBIX_ Server can’t Ping, VIP 192.168.74.150.
Wait for 60s, VIP (192.168.74.150) will appear in standby ZABBIX_ server （192.168.74.148 ）
The VIP switch is successful. Refresh the interface to see if you can access ZABBIX_ server
Normal interface access
Master ZABBIX_ The server is down, so it can’t get the data.
This case can ensure that ZABBIX_ Server service for high availability. But there is one drawback: two ZABBIX_ The server service uses the same database server. If the database server fails. Data will also be unavailable. So it’s better to make the database highly available.
Zabbix_ How to solve the Chinese garbled code when the server executes the window script
Employees of enterprise a are flourishing in ZABBIX_ On the server, a remote window host script is called through the agent, but because the output of the script is in Chinese, ah Wang is in ZABBIX_ Server interface to see the content is a string of garbled characters, he did not know whether the remote script execution success. So he wanted to change the output of the script to English prompt success or error. But because the script was not written by Awang. So he didn’t know where to modify the script to make it appear in English. Therefore, the purpose of this paper is to solve the problem of ZABBIX_ Server calls the remote script of window with Chinese display garbled problem.
Step 1: write a bat script to output Chinese on the window
Is it possible to output results by executing bat script on the window server
The bat script is executed on the window host, and the result is output normally.
Step 2: in ZABBIX_ Create a key on the server to execute the script of the file
Because the script is a script on the window, you should choose to execute it on the ZABBIX client
Execute the script on the host where the script is stored
The result after execution is shown in the following figure. The Chinese display is garbled and the number display is normal (the English display is also normal)
Step 3: write the bat script of transcoding to realize the normal output of Chinese
The path of the file should use absolute value, otherwise an error will be reported when calling
The first line: output the content of the test. Bat script execution result to the 1. TXT file
On the second line, transcode the test. TXT file to UTF 8 format
(note that in the second line, the source file name cannot be the same as the name after transcoding, otherwise the transcoded file will be empty.)
That is, you can’t use PowerShell – C “get content F: desktop C disk 1. TXT | out file – encoding utf8 F: desktop C disk 1. TXT”
The third line is to check the execution of the test. Bat script
The fourth and fifth lines delete the file just generated
Modify and execute the window script again
OK, it’s done. The Chinese display is normal.