To implement Zabbix LLD monitoring, you typically need two scripts, a custom discovery check script, and output in JSON format, and the other is the script to get the valus of the monitoring item.
The custom discovery checks script output results in the following form:
{"Data": [{"{#APP_NAME}": "Appcmdb"}, {"{#APP_NAME}": "Appmerchantweba Pi "}, {" {#APP_NAME} ":" Appnoc "}, {" {#APP_NAME} ":" Appsso "} ]}
Writing such a script in Python is very handy, so how do you write it with the shell?
Let's look at an example:
The company needs to monitor the app processes that produce the server, monitor the CPU usage, memory usage size, and process startup number.
The app directory is placed in the/workspace/carkey/directory
[Email protected]:/workspace/carkey# lsaaa appcmdb Appnoc appsso appyunboweb Get_app.shap ache-tomcat-8.0.28 Appmerchantwebapi Apppms apptest bbb.py
All apps start with directories that are in the Company app directory, others are not. These apps are Java programs, and the process name can be started after
Ps-ef | grep app_name matches to, for example
[Email protected] ~]# Ps-ef | grep Apppms
Root 4698 1 0 Jul13? 00:00:28/usr/java/latest/bin/java-djava.util.logging.config.file=/workspace/carkey/apppms/latest/conf/ Logging.properties-djava.util.logging.manager=org.apache.juli.classloaderlogmanager-server-xx:permsize=256m-xx : MAXPERMSIZE=1024M-XMS512M-XMX1024M-XX:MAXNEWSIZE=256M-XMS512M-XMX1024M-SERVER-XX:+USEPARALLELGC- djava.endorsed.dirs=/usr/local/tomcat/endorsed-classpath/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/ bin/tomcat-juli.jar-dcatalina.base=/workspace/carkey/Apppms/latest-dcatalina.home=/usr/local/tomcat- djava.io.tmpdir=/workspace/carkey/Apppms/latest/temp org.apache.catalina.startup.Bootstrap start
It is now necessary to monitor all app processes. So, we get all the process names through the ls/workspace/carkey/command. And make the process name in JSON format:
{"Data": [{"{#APP_NAME}": "Appcmdb"}, {"{#APP_NAME}": "Appmerchantweba Pi "}, {" {#APP_NAME} ":" Appnoc "}, {" {#APP_NAME} ":" Appsso "} ]}
The following is the shell script content:
#!/bin/bashexport lang=en_us. Utf-8unset lc_alla= (' ls /workspace/carkey | grep ' ^app " 2>/dev/null ') b= () for i in ${a[@]}; do if [ ' ls / workspace/carkey/$i |grep latest|wc -l ' -gt 0 ];then b= ("${b[@]}" "$i") fidonelength=${#b [@]}printf "{\ n" printf ' \ t ' "\" data\ ": [" for ((i=0;i< $length; i++)) do printf ' \n\t\t{' printf "\" {#APP_NAME}\ ": \" ${b[$i]}\ "}" if [ $i -lt $[$length-1] ];then printf ', ' fidoneprintf "\n\t]\n" printf "}\n"
The shell is primarily implemented using arrays, where the basics of arrays are sorted out.
1, the definition of the array
[[email protected] ~]# a= (1 2 3 4 5) [[email protected] ~]# echo $a 1[[email protected] ~]# echo ${a[*]}1 2 3 4 5[[email Pro Tected] ~]# Echo ${a[@]}1 2 3 4 5
A pair of parentheses represents an array, and the array elements are separated by a "space" symbol. View all elements of an array, with ${a[*]} or ${a[@]}
2. View an element in an array by subscript
[[email protected] ~]# echo ${a[0]}1[[email protected] ~]# echo ${a[1]}2[[email protected] ~]# echo ${a[4]}5[[email Protec Ted] ~]# Echo ${a[5]}
Arrays are ordered, as is the case with lists and tuples in Python, where the subscript of an array starts with 0.
3. Get the array length
[Email protected] ~]# echo ${#a [@]}5[[email protected] ~]# echo ${#a [*]}5
4. Assigning values to arrays
[Roo[email protected] ~]# a[1]=10[[email protected] ~]# echo ${a[1]}10[[email protected] ~]# echo ${a[*]}1 10 3 4 5
Arrays can be modified, as in Python, but tuples in Python are not modifiable
5. Delete
[Email protected] ~]# a= (1 2 3 4 5)
[Email protected] ~]# unset a
[[email protected] ~]# echo ${a[*]}
[Email protected] ~]# a= (1 2 3 4 5)
[Email protected] ~]# unset a[1]
[[email protected] ~]# echo ${a[*]}
1 3 4 5
6 , replace
[Email protected] ~]# a= (1 2 3 4 5)
[[email protected] ~]# echo ${A[@]/3/10}
1 2 10) 4 5
[[email protected] ~]# echo ${a[@]}
1 2 3) 4 5
[[email protected] ~]# a= (${A[@]/3/10})
[[Email protected]
The call method is: ${array name [@ or *]/find character/substitution character} This operation will not change the original array contents, if you need to modify, you can see the above example, redefine the data.
7. Array append element
[[email protected] ~]# b= (${b[@]} 1) [[email protected] ~]# echo ${b[@]}1[[email protected] ~]# b= (${b[@]} 2) [[Email Protec Ted] ~]# Echo ${b[@]} 1 2[[email protected] ~]# b= (${b[@]} 3) [[email protected] ~]# echo ${b[@]} 1 2 3
The shell script above is using this method to regenerate an array and save the app_name that match the condition.
Python script:
#!/usr/bin/env Pythonimport commands,jsonapp_str = Commands.getoutput (' ls/workspace/carkey/|grep ' ^App ') new_app_ List = []app_list = App_str.split (' \ n ') for I in app_list:com = "ls/workspace/carkey/%s | grep latest "% I commvalue=commands.getstatusoutput (COM) if commvalue[0] = = 0:new_app_list.append (I) data={}l Is=[]for app in new_app_list:dic={} dic["{#APP_NAME}" = App Lis.append (DIC) data["data"] = Lisprint json.dumps ( Data,sort_keys=true, indent=4)
Reference: http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html
This article is from the "Zengestudy" blog, make sure to keep this source http://zengestudy.blog.51cto.com/1702365/1826725
The shell script implements LLD monitoring