A few changes were made to the monitoring scripts today, and then all of a sudden, the monitoring was completely invalidated. A half-day problem is still unknown. Finally found it was a step on several times the old pit.
is the script written in the configuration file for debugging convenience written relative path, but on-line not aware of the software automatic execution (such as Zabbix ExternalCheck, although all scripts can be placed in a unified directory, Giving a kind of this directory is the illusion of the working directory when Zabbix performs an external check, but it's not actually. ), the working directory that is automatically executed is not necessarily the current directory. In this pit has been stepped several times, the following examples and solutions:
import CONFIGPARSERCF = Configparser () conf_file = " " # When writing scripts locally for debugging convenience, create a configuration file in the same directory as the script to configure cf.read (conf_file) # face conf_file do some processing # The same script, let Zabbix to run their own error??? Then go to the directory to do their own manual execution, no problem??? is the path of the pot! # change path to absolute path ok conf_file = '
Think about it, this pot is actually configparser this module itself a small "flaw" bar. because Configparser.configparser (). Read (path), there is no error even if path does not exist. If there is an error, we can locate the problem quickly. So remember to remember, before using Configparser must os.path.isfile (path) Check!
If you want to preserve the flexibility of configuration files and scripts on porting, you can do this:
Work_dir = Os.path.dirname (Os.path.abspath (__file__= Os.path.join (Work_dir,' server.conf')# so that you can guarantee the absolute path of conf_file, and will not write dead.
Similar operations have been written many times and are no longer repeated.
"Python" Profile relative path & working directory for automatic software execution