zabbix 中的 python 监控脚本

1、kubernetes 集群状态监控脚本

#!/usr/bin/env python
#coding:utf-8
import subprocess
success_list = []
error_list= []
def get_status():
    obj = subprocess.Popen(("curl -sXGET http://127.0.0.1:8080/api/v1/nodes"),shell=True,stdout=subprocess.PIPE)
    data = obj.stdout.read()
    global false, null, true
    false = False
    true=True
    null=''
    data1 = eval(data)
    data2 = data1.get('items')
    for i in data2:
        data3 = i.get('status')
        for i in data3.get('conditions'):
            if i.get('reason') == 'KubeletReady':
                if i.get('type') == "Ready":
                    if i.get('status') == 'True':
                        success_list.append(i.get('status'))
                    elif i.get('status') == 'False':
                        error_list.append(i.get('status'))
                    else:
                        break
                else:
                    error_list.append(i.get('status'))
            else:
                error_list.append(i.get('status'))

def count_status():
    if len(error_list) == 0:
        print 50
    else:
        print 100

def main():
    get_status()
    count_status()

if __name__ == "__main__":
    main()

2、监控 MongodbDB 复制集群状态

#!/bin/env python
#coding:utf-8

import subprocess
success_list = []
error_list= []

def get_mongodb_status():
    obj = subprocess.Popen(("echo 'rs.status()' | /usr/local/mongodb/bin/mongo -u user -p wswd --authenticationDatabase admin \
| grep health | awk -F':' '{print 2}' | awk -F',' '{print1}'"),shell=True,stdout=subprocess.PIPE)
    restful = obj.stdout.read()
    data = restful.split()
    for i in data:
        if i == "1":
            success_list.append(i)
        else:
            error_list.append(i)

def count_status():
    if len(error_list) > 0:
        print(100)
    else:
        print(50)

def main():
    get_mongodb_status()
    count_status()

if __name__ == "__main__":
    main()

3、监控 Redis 列表长度

#!/usr/bin/env python
#coding:utf-8

import redis
def redis_conn():
    pool = redis.ConnectionPool(host="10.20.0.252",port=6379,db=0)
    conn = redis.Redis(connection_pool=pool)
    data = conn.llen('list')
    print(data)
redis_conn()

4、监控 ELK 集群状态

#!/usr/bin/env python
#coding:utf-8

import subprocess
false="false"
obj = subprocess.Popen(("curl -sXGET http://10.20.3.128:9200/_cluster/health?pretty=true"),shell=True, stdout=subprocess.PIPE)
data = obj.stdout.read()
data1 = eval(data)
status = data1.get("status")
if status == "green":
    print "100"
else:
    print "50"

5、监控 RabbitMQ 集群节点状态

#cat /etc/zabbix/zabbix_agentd.d/rabbit_cluster_monitor.py
#!/bin/env python
#coding:utf-8

import  subprocess
running_list = []
error_list = []
false="false"
true="true"

def get_status():
    obj = subprocess.Popen(("curl -sXGET -u guest:guest http://10.20.3.171:15671/api/nodes"),shell=True, stdout=subprocess.PIPE)
    data =  obj.stdout.read()
    data1 = eval(data)
    for i in data1:
        if i.get("running") == "true":
            running_list.append(i.get("name"))
        else:
            error_list.append(i.get("name"))

def count_server():
    if len(running_list) < 3: #可以判断错误列表大于0或者运行列表小于3,3未总计的节点数量
        print 100 #100就是集群内有节点运行不正常了
    else:
        print 50  #50为所有节点全部运行正常

def main():
    get_status()
    count_server()
if __name__ == "__main__":
    main()
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇