linux监控脚本是否运行状态,Linux Shell脚本之通过json判断应用程序内部运行状态...
之前写过一篇《Zabbix监控之Linux命令行/Shell脚本解析json》,文章提到一种“利于Zabbix监控报警的json数据格式”,便于运维人员通过API获取应用程序中的运行状态、内部依赖关系等。但有的开发人员没有按照这种格式来,而是采用了下面的这种json数据格式:
成功:{
"errcode": "0",
"errmsg": "ok"
}
失败:{
"errcode": "1",
"errmsg": {
"Mysql": {
"code": "0",
"msg": "OK"
},
"Memcache": {
"code": "0",
"msg": "0"
},
"MQ": {
"code": "0",
"msg": "OK"
},
"BS": {
"code": "1",
"msg": "os与bs连接出现故障"
},
"Redis": {
"code": "0",
"msg": "OK"
}
}
}
成功:{
"errcode": "0",
"errmsg": {
"Mysql": {
"code": "0",
"msg": "OK"
},
"Memcache": {
"code": "0",
"msg": "0"
},
"MQ": {
"code": "0",
"msg": "OK"
},
"BS": {
"code": "0",
"msg": "ok"
},
"Redis": {
"code": "0",
"msg": "OK"
}
}
}
失败:{
"errcode": "1",
"errmsg": {
"Mysql": {
"code": "0",
"msg": "OK"
},
"Memcache": {
"code": "0",
"msg": "0"
},
"MQ": {
"code": "0",
"msg": "OK"
},
"BS": {
"code": "1",
"msg": "os与bs连接出现故障"
},
"Redis": {
"code": "0",
"msg": "OK"
}
}
}
由于今晚就要更新包含这段代码的应用,有可能来不及修改,因此我只能重新修改一下先前写好的脚本,现将脚本展示如下:#!/bin/bash
# Shell Script Details
# Name: getdata_gwservice.sh
# Version: 1.0
# Visual Name(human readable format): get data from Web Service(GWService)
# Shell Script Function: get data from Web Service with curl, data format is json
# Author: Guodong Ding
# Date: Thu Aug 20 09:56:26 CST 2015
# parameter is necessary
[ ! -z "$1" ] || exit 1
# resolve "jq" command for parse json
if [[ ! `which jq` ]]; then
echo "jq is not installed on target system, please install it first! "
# adaptive for CentOS or Ubuntu
apt-get -qq -y install jq || yum -y install jq
[ $? -ne 0 ] && echo "Trying install jq failed! ";echo "Trying install jq successfully! "
echo "$(which jq) is found!"
echo 1
fi
# refactoring curl CLI
curl=$(which curl)
postdata="accesscode=somecode&method=someapiname"
# test environment
url=http://hostname/url/api
# production environment
#url=http://hostname/url/api
# TODO
# why not execute a string as command?
# curlcli="$curl -d \"$postdata\" $url 2>/dev/null"
# make repeated cli to a function
function curlcli() {
$curl -m 10 --connect-timeout 10 -s -d "$postdata" $url 2>/dev/null
# For test purpose
#echo '{"errcode":"1","errmsg":{"Mysql":{"code":"0","msg":"OK"},"Memcache":{"code":"0","msg":"0"},"MQ":{"code":"0","msg":"OK"},"BS":{"code":"1","errmsg":"os与bs连接出.故障"},"Redis":{"code":"0","errmsg":"OK"}}}'
[ $? -ne 0 ] && exit $?
}
# TODO
# How to merge function totalstatus() and function total_error_status() into a function?
function totalstatus() {
# another awk expression is: awk '{print int($1)}'
curlcli | jq ".errcode" | awk -F '"' '/[0-9]/ {print $2}'
[ $? -ne 0 ] && exit $?
}
function total_error_status() {
outputswitch=`curlcli | jq ".errcode" | awk -F '"' '/[0-9]/ {print $2}'`
if [[ $outputswitch -eq 1 ]]; then
curlcli | jq ".errcode" | awk -F '"' '/[0-9]/ {print $2}' >/dev/null 2>&1
else
curlcli | jq ".errcode" | awk -F '"' '/[0-9]/ {print $2}'
fi
[ $? -ne 0 ] && exit $?
# TODO
# why we need a return for this function ? because we will use "||" to determine result of CLI
# by the way , "||" is depended on the result which returned with keyword "return"
# tested pass with return a numeric argument lager than 1
return `curlcli | jq ".errcode" | awk -F '"' '/[0-9]/ {print $2}'`
}
function mysqlstatus() {
curlcli | jq ".errmsg.Mysql.code" | awk -F '"' '/[0-9]/ {print $2}'
[ $? -ne 0 ] && exit $?
}
function memcachedstatus() {
curlcli | jq ".errmsg.Memcache.code" | awk -F '"' '/[0-9]/ {print $2}'
[ $? -ne 0 ] && exit $?
}
function messagequeuestatus() {
curlcli | jq ".errmsg.MQ.code" | awk -F '"' '/[0-9]/ {print $2}'
[ $? -ne 0 ] && exit $?
}
function businessservicestatus() {
curlcli | jq ".errmsg.BS.code" | awk -F '"' '/[0-9]/ {print $2}'
[ $? -ne 0 ] && exit $?
}
function redisstatus() {
curlcli | jq ".errmsg.Redis.code" | awk -F '"' '/[0-9]/ {print $2}'
[ $? -ne 0 ] && exit $?
}
case $1 in
totalstatus )
totalstatus
;;
mysqlstatus )
total_error_status || mysqlstatus
;;
memcachedstatus )
total_error_status || memcachedstatus
;;
messagequeuestatus )
total_error_status || messagequeuestatus
;;
businessservicestatus )
total_error_status || businessservicestatus
;;
redisstatus )
total_error_status || redisstatus
;;
esac#!/bin/bash
# Shell Script Details:
# Name: getdata_gwservice.sh
# Version: 2.0
# Visual Name(human readable format): get data from Web Service(GWService) for Zabbix monitoring
# Shell Script Function: get data from Web Service with curl, data format is json
# Author: Guodong Ding
# Date: Thu Aug 20 09:56:26 CST 2015
# parameter is necessary
[ ! -z "$1" ] || exit 1
# resolve "jq" command for parse json
if [[ ! `which jq` ]]; then
echo "jq is not installed on target system, please install it first! "
# adaptive for CentOS or Ubuntu
apt-get -qq -y install jq || yum -y install jq
[ $? -ne 0 ] && echo "Trying install jq failed! ";echo "Trying install jq successfully! "
echo "$(which jq) is found!"
echo 1
fi
# refactoring curl CLI
curl=$(which curl)
postdata="accesscode=somecode&method=someapiname"
# test environment
url=http://hostname/url/api
# production environment
#url=http://hostname/url/api
# TODO
# why not execute a string as command?
# curlcli="$curl -d \"$postdata\" $url 2>/dev/null"
# make repeated cli to a function
function curlcli() {
$curl -m 10 --connect-timeout 10 -s -d "$postdata" $url 2>/dev/null
# For test purpose
#echo '{"errcode":"1","errmsg":{"Mysql":{"code":"0","msg":"OK"},"Memcatch":{"code":"0","msg":"0"},"MQ":{"code":"0","msg":"OK"},"BS":{"code":"1","errmsg":"os与bs连接出.故障"},"Redis":{"code":"0","errmsg":"OK"}}}'
[ $? -ne 0 ] && exit $?
}
function totalstatus() {
# another awk expression is: awk '{print int($1)}'
curlcli | jq ".errcode" | awk -F '"' '/[0-9]/ {print $2}'
[ $? -ne 0 ] && exit $?
# TODO, TOREMEMBER
# Why we need a return for this function? because we will use "||" to determine result of CLI,
# such as "totalstatus >/dev/null 2>&1 || mysqlstatus".
# By the way , "||" is depended on the result which returned with keyword "return",
# tested pass with return a numeric argument lager than 1 , such as 1001, 9003, etc
return `curlcli | jq ".errcode" | awk -F '"' '/[0-9]/ {print $2}'`
}
function mysqlstatus() {
curlcli | jq ".errmsg.Mysql.code" | awk -F '"' '/[0-9]/ {print $2}'
[ $? -ne 0 ] && exit $?
}
function memcachedstatus() {
curlcli | jq ".errmsg.Memcache.code" | awk -F '"' '/[0-9]/ {print $2}'
[ $? -ne 0 ] && exit $?
}
function messagequeuestatus() {
curlcli | jq ".errmsg.MQ.code" | awk -F '"' '/[0-9]/ {print $2}'
[ $? -ne 0 ] && exit $?
}
function businessservicestatus() {
curlcli | jq ".errmsg.BS.code" | awk -F '"' '/[0-9]/ {print $2}'
[ $? -ne 0 ] && exit $?
}
function redisstatus() {
curlcli | jq ".errmsg.Redis.code" | awk -F '"' '/[0-9]/ {print $2}'
[ $? -ne 0 ] && exit $?
}
case $1 in
totalstatus )
totalstatus
;;
mysqlstatus )
mysqlstatus
;;
memcachedstatus )
memcachedstatus
;;
messagequeuestatus )
messagequeuestatus
;;
businessservicestatus )
businessservicestatus
;;
redisstatus )
redisstatus
;;
* )
exit 1
;;
esac
tag:Linux命令行解析json,Linux Shell解析json,Zabbix业务监控,Zabbix自定义用户参数,Zabbix API
--end--
linux监控脚本是否运行状态,Linux Shell脚本之通过json判断应用程序内部运行状态...相关推荐
- linux脚本监控某一进程,linux监控某个进程的运行shell脚本
该脚本实现了对指定进程名的进程进行每隔2的扫描监控,一旦发现进程不存在便重新启动. #!/bin/sh a=10; while [[ @a -gt 5 ]]; do if test $(pgrep - ...
- 监控mysql的shell脚本_监控MySQL主从状态的shell脚本
分享一个Linux下,监控MySQL主从状态及配合企业微信机器人报警的Shell脚本 SLAVE_IP:为监控的主机IP USER:为msyql用户 PASSWORD:为mysql密码 WHEREIS ...
- linux编写复制脚本程,常用的Shell脚本
1.通过位置变量创建linux系统账户及密码 $1 是执行脚本的第一个参数,$2 是执行脚本的第二个参数 1 #!/bin/bash 2 #Author: Peter zh 3 #Blog: http ...
- linux 脚本实现物理备份,shell脚本实现系统监视统计与数据备份
知识内容:*管理统计信息*执行备份*管理用户对于linux SA来说,没啥比shell脚本编程更有用的了.linux系统每天都有很多任务需要做好,从监视系统 磁盘空间.系统用户到备份系统重要文件.通过 ...
- linux 查重脚本,Linux脚本学习必经之路:Shell脚本实例分享
写shell脚本还是一样,思路第一,语法其次,下面分享几个脚本实例,大家主要是理解一下里面的思路,语法稍微注意一下. 1. 观察数字,输出包含前6个数字在内的16个数字 观察以下数字,看有什么规律: ...
- linux 脚本中=$4,shell脚本实例,通向shell脚本大师的必经之路
概述 读书百遍其义自见,shell脚本也是,只要例子看得多了,自然就知道怎么写了.这里主要整理了20几个例子,因为内容比较多,所以分了几次来做介绍了.下面的实例最好先自己思考怎么去实现,然后再看下实现 ...
- linux脚本对磁盘分区,shell 脚本实战笔记(4)--linux磁盘分区重新挂载
背景: Hadoop的HDFS文件系统的挂载, 默认指定的文件目录是/mnt/disk{N}. 当运维人员, 不小心把磁盘挂载于其他目录, 比如/mnt/data, /mnt/disk01, /mnt ...
- linux下防止ARP攻击的shell脚本
防止ARP攻击的shell脚本,使用命令route.grep.ifconfig等,需要的朋友可以参考下就不废话了,直接上代码了. #!/bin/bash declare gw=`route -n | ...
- linux过滤脚本中的字段,Shell脚本中常用的文本过滤命令
在Linux运维日常工作中,Shell脚本的使用如家常便饭一样,须做到顺手拈来,文本处理所占比重更是不容小视,而文本处理中,稍具难度的莫过于文本的过滤.今天我们要通过这一讲,掌握文本过滤的常用命令,有 ...
最新文章
- 读书笔记之《习惯的力量》
- Java自带的性能监测工具之jinfo
- linux 连接远程命令行,screen命令行远程连接
- 动态域名作为dga的做法
- UISwitch,UISegmentedControl及UISlider的初步学习
- 微软发布Azure Service Fabric Mesh公开预览版
- 阿里格林深瞳计算机视觉岗实习面经
- php查询记录是否存在,php – 如果记录存在,我可以更新记录,如果不存在,可以在单个查询中更新多行吗?...
- 我和计算机比本领教案反思,《比本领》教学设计及反思
- windows7下bcdedit出现“拒绝访问”解决办法
- 如何检测圣诞树? [关闭]
- Nagios 3 Centreon 2 RC5 安装与配置(1)
- 用ffmpeg在命令行下,对文件进行转码H264
- 郑州大学远程教育学院C语言程序设计题库(二)
- 计算机对用户的操作做出反应,云南省计算机二级VB考试真题题库
- Maxon伺服驱动EPOS2 24/5 配置
- 轩辕剑--资料集(三)
- 何恺明组新论文:只用ViT做主干也可以做好目标检测
- Ribbit Developer Platform介绍。
- INFO Starting development server...98% after emitting CopyPlugin ERROR Failed to compile with