设为首页 收藏本站
查看: 7369|回复: 0

[经验分享] zabbix替代smokeping的解决方案

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-6-23 11:10:32 | 显示全部楼层 |阅读模式
本帖最后由 12ewe 于 2014-6-23 11:12 编辑

最近要部署smokeping,想使用nginx的fastcgi的方式来部署,结果perl的执行的效率真是大失所望,就想用zabbix来实现smokeping。

python 环境2.6
参考的zabbix 社区
https://www.zabbix.com/forum/showthread.php?t=31147

实现的原理使用zabbix_sender主动向zabbixserver发送数据,zabbix就能采集到数据啦
文件清单:
     all  create_graph.py  create_other_screen.py  define monitor.py  zbxsmokepingprimary        zbx_export_templates.xml

文件说明:
    all: 所要监控ip地址池,这个文件monitor.py和create_graph.py都会在这里读取ip,create_graph.py是个多线程的脚本用于发送all的所有ip到zabbixserver,monitor.py去读取all文件的ip生成所需要监控的主
    define: 定义客户的ip地址,这个文件会被create_create_other.py读取,create_other_screen.py实现读取define文件中的ip地址实现自动生成带客户screen,手动生成的话会该觉很头痛
   zbxsmokepingprimary: 是外部的执行的脚本需要在zabbix_server设置
   zbx_export_templates.xml 模版文件


设置的选项:
      zabbix_server:
                       开启这个选项ExternalScripts=/usr/local/share/zabbix/externalscripts
     zbxsmokepingprimary
                       ZBXSERVER:zabbix服务器的ip地址
                       FPING:fping的位置
                       ZBXSENDER:zabbix_sender的位置
            zabbix主机监控有个类型为external check 选项会去执行 zbxsmokepingprimary这个脚本,这个脚本需要有执行的权限

     monitor.py和create_other_screen.py需要修改的zabbix url地址,帐号和密码
   create_graph.py:
         num_threads=10 设置初始化的线程个数,ip多的话初始线程可以跟ip个数相等
软件包:
     supervisor:python的程序,监控脚本,使之一直运行,supervisor需要执行的文件为create_graph.py,这个就相当于守护进程来使用
     pyzabbix:python连接zabbix的api

monitor.py 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python

import pyzabbix


class zabbix(object):
    def __init__(self):
        self.all=open('all')

    def _login(self):
        zapi= pyzabbix.ZabbixAPI('url')  #your zabbix url
        zapi.login('user','password') # you zabbix user and password the user have write privilege
        return zapi

    def _get_template(self):
        temp=self._login().template.get(output='extend',filter={'host':'Template_SmokePrimary'})
        return  temp[0]['templateid']
     
    def _get_group(self):
        if  not self._login().hostgroup.exists(name='ping_check'):
            
            self._login().hostgroup.create(name='ping_check')
        group=self._login().hostgroup.get(output='extend',filter={'name':'ping_check'})
        return group[0]['groupid']

    def _create_host(self):
        for x in self.all:
            if not self._login().host.exists(host=x.strip()):
                try:
                    self._login().host.create(
                                host=x.strip(),
                                interfaces={"type":1, "main":1, "useip":1, "ip":"127.0.0.1","dns":"","port":"10050"},
                                groups={"groupid":str(self._get_group())},
                                templates={"templateid":str(self._get_template())},
                                )
                except Exception,e:
                    print e
            
                 
     

    def main(self):
#       self._get_group()
#       self._get_template()
        self._create_host()

if __name__ == '__main__':
    a=zabbix()
    a.main()   





create_other_screen.py代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python

import pyzabbix
import re

class zabbix(object):
        def __init__(self):
                self.define=open('define')
        self.dynamic=0
        self.columns=2
        for x in self.define:
            self.name=x.strip()
            break         #the first line is you screen's name

        def _login(self):
                zapi= pyzabbix.ZabbixAPI('url') #you zabbix server url
                zapi.login('admin','zabbix')  #your zabbix user and password
                return zapi
     
    def _get_host(self):
        host_ids=[]
        for x in self.define:
            if not re.search('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',x.strip()):
                continue
            if self._login().host.exists(host=x.strip()):
                hosts=self._login().host.get(filter={'host':[x.strip()]})
                host_ids.append(hosts[0]['hostid'])
        return  host_ids

    def _get_graph(self):
        graph_ids=[]
        for x in self._get_host():
            graph=self._login().graph.get(hostids=x)
            graph_ids.append(graph[0]['graphid'])

        graph_list=[]
        x = 0
        y = 0   
        for graph in sorted(graph_ids):
                        graph_list.append({
                                        "resourcetype":'0',
                                        "resourceid": graph,
                                        "width": "600",
                                        "height": "100",
                                        "x": str(x),
                                        "y": str(y),
                                        "colspan": "0",
                                        "rowspan": "0",
                                        "elements": "0",
                                        "valign": "0",
                                        "halign": "0",
                                        "style": "0",
                                        "url": "",
                                        "dynamic": str(self.dynamic)
                                        })
                        x += 1
                        if x == int(self.columns):
                                x = 0
                                y += 1      
        return  graph_list   
     
    def _get_screen(self):
        id=self._login().screen.get(output='extend',filter={'name':self.name})
        return id[0]['screenid']   
    def _create_screen(self):
        if self._login().screen.exists(name=self.name):
            self._login().screen.delete(str(self._get_screen()))
            #return 0
                graphids=self._get_graph()
                columns = int(self.columns)
                if len(graphids) % self.columns == 0:
                        vsize = len(graphids) / self.columns
                else:
                        vsize = (len(graphids) / self.columns) + 1
                self._login().screen.create(name=self.name,hsize=self.columns,vsize=vsize,screenitems=graphids)

    def main(self):
#       self._get_graph()
#       self._get_screen()
        self._create_screen()

if __name__ == '__main__':
    a=zabbix()
    a.main()





create_graph.py 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python

import subprocess
from threading import Thread
from  Queue import Queue
import time

f=open('all')
d=[]
for x in f:
    d.append('/usr/local/share/zabbix/externalscripts/zbxsmokepingprimary  %s 10 1000 32 %s' % (x.strip(),x.strip()))

queue=Queue()
def do(i,q):
    while True:
        a=q.get()
        ret=subprocess.call(a,shell=True)

        q.task_done()

num_threads=10  #init thread number

for i in range(num_threads):
    worker = Thread(target=do,args=(i,queue))
    worker.setDaemon(True)
    worker.start()
         
     
for x in d:
    queue.put(x)

queue.join()

time.sleep(3)



我这边对于主机在screen中没有排序,每个人的需求可能不同
下面附上脚本文件,软件包需要自己去下载。。。。。

下面是我在百度和163域名解析到的ip地址生成的图片



脚本和文件.rar (13.99 KB, 下载次数: 124) QQ截图20140623111039.png





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-20919-1-1.html 上篇帖子: zabbix发送报警的几种方法 下篇帖子: Zabbix --> 客户端安装以及如何添加数据库的动态监控视图
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表