docker zabbixモニタリングの構築
1、ディレクトリの作成[root@localhost script]# mkdir/usr/local/zabbix/etc/script
2、コンテナ発見スクリプトの編集[root@localhost script]# vim docker_discovery.py#!/usr/bin/env python import osimport simplejson as jsont=os.popen("""sudo docker ps |grep -v 'CONTAINER ID'|awk {'print $NF'} """)container_name = []for container in t.readlines():r = os.path.basename(container.strip())container_name += [{'{#CONTAINERNAME}':r}]print json.dumps({'data':container_name},sort_keys=True,indent=4,separators=(',',':'))
3、インストール環境とテスト自動発見pip install dockerpip install simplejsonchmod 757 docker_discovery.pychown zabbix:zabbix/usr/local/zabbix/etc/script -R
4、容器監視テストを追加[root@localhost script]# vim docker_monitor.py #!/usr/bin/env pythonimport dockerimport sysimport subprocessimport os
5、web側作成テンプレート5.1、構成-』テンプレート-』作成テンプレート(cpu_percent mem_percent mem_usage mem_limit)Template name:Template Docker Auto Discovery
6、zabbixクライアントのインストール
2、コンテナ発見スクリプトの編集[root@localhost script]# vim docker_discovery.py#!/usr/bin/env python import osimport simplejson as jsont=os.popen("""sudo docker ps |grep -v 'CONTAINER ID'|awk {'print $NF'} """)container_name = []for container in t.readlines():r = os.path.basename(container.strip())container_name += [{'{#CONTAINERNAME}':r}]print json.dumps({'data':container_name},sort_keys=True,indent=4,separators=(',',':'))
3、インストール環境とテスト自動発見pip install dockerpip install simplejsonchmod 757 docker_discovery.pychown zabbix:zabbix/usr/local/zabbix/etc/script -R
zabbix , /etc/sudoers,
zabbix ALL=(root) NOPASSWD:/usr/bin/docker,/usr/bin/python,/usr/local/zabbix/etc/script/docker_discovery.py
:
vim /usr/local/zabbix/etc/zabbix_agentd.conf
UserParameter=docker_discovery,sudo /usr/bin/python /usr/local/zabbix/etc/script/docker_discovery.py
:
zabbix_get -s 192.168.199.133 -k docker_discovery
4、容器監視テストを追加[root@localhost script]# vim docker_monitor.py #!/usr/bin/env pythonimport dockerimport sysimport subprocessimport os
def check_container_stats(container_name,collect_item):
#docker_client = docker_client.containers.get(container_name)
container_collect=docker_client.containers.get(container_name).stats(stream=True)
old_result=eval(container_collect.next())
new_result=eval(container_collect.next())
container_collect.close()
if collect_item == 'cpu_total_usage':
result=new_result['cpu_stats']['cpu_usage']['total_usage'] - old_result['cpu_stats']['cpu_usage']['total_usage']
elif collect_item == 'cpu_system_usage':
result=new_result['cpu_stats']['system_cpu_usage'] - old_result['cpu_stats']['system_cpu_usage']
elif collect_item == 'cpu_percent':
cpu_total_usage=new_result['cpu_stats']['cpu_usage']['total_usage'] - old_result['cpu_stats']['cpu_usage']['total_usage']
cpu_system_uasge=new_result['cpu_stats']['system_cpu_usage'] - old_result['cpu_stats']['system_cpu_usage']
cpu_num=len(old_result['cpu_stats']['cpu_usage']['percpu_usage'])
result=round((float(cpu_total_usage)/float(cpu_system_uasge))*cpu_num*100.0,2)
elif collect_item == 'mem_usage':
result=new_result['memory_stats']['usage']
elif collect_item == 'mem_limit':
result=new_result['memory_stats']['limit']
elif collect_item == 'network_rx_bytes':
result=new_result['networks']['eth0']['rx_bytes']
elif collect_item == 'network_tx_bytes':
result=new_result['networks']['eth0']['tx_bytes']
elif collect_item == 'mem_percent':
mem_usage=new_result['memory_stats']['usage']
mem_limit=new_result['memory_stats']['limit']
result=round(float(mem_usage)/float(mem_limit)*100.0,2)
return result
if __name__ == "__main__":
docker_client = docker.DockerClient(base_url='unix://var/run/docker.sock', version='1.27')
container_name=sys.argv[1]
collect_item=sys.argv[2]
print check_container_stats(container_name,collect_item)
[root@localhost script]# chmod 757 docker_monitor.py
:vim /usr/local/zabbix/etc/zabbix_agentd.conf
UserParameter=docker_status[*],sudo /usr/bin/python /usr/local/zabbix/etc/script/docker_monitor.py $1 $2
:zabbix_get -s 192.168.199.133 -k docker_status[mysql-sonar,cpu_total_usage]
5、web側作成テンプレート5.1、構成-』テンプレート-』作成テンプレート(cpu_percent mem_percent mem_usage mem_limit)Template name:Template Docker Auto Discovery
5.2、
5.3、 :
:
Name:collect docker container use resource
Type:Zabbix agent
Key:docker_discovery
:
{#CONTAINERNAME}
5.4、
Name:container:{#CONTAINERNAME}:cpu_percent
Type:Zabbix agent
Key:docker_status[{#CONTAINERNAME},cpu_percent]
Type:Numeric (float)
Units:%
Name:container:{#CONTAINERNAME}:mem_percent
Type:Zabbix agent
Key:docker_status[{#CONTAINERNAME},mem_percent]
Type:Numeric (float)
Units:%
Name:container:{#CONTAINERNAME}:mem_usage
Type:Zabbix agent
Key:docker_status[{#CONTAINERNAME},mem_usage]
Type: ( )
Units:
Name:container:{#CONTAINERNAME}:mem_limit
Type:Zabbix agent
Key:docker_status[{#CONTAINERNAME},mem_limit]
Type: ( )
Units:
5.5、
5.5、
Name:container:{#CONTAINERNAME}:cpu
:
6、zabbixクライアントのインストール
:
#groupadd zabbix
#useradd -g zabbix -m zabbix
#./configure --prefix=/usr/local/zabbix --enable-agent
#make install
#vi /usr/local/zabbix/etc/zabbix_agentd.conf
Server= ip.ip.ip.ip # ip
ServerActive= ip.ip.ip.ip # ip
Hostname=client1 # hostname
#cp zabbix-3.0.4/misc/init.d/tru64/zabbix_agentd /etc/init.d/
#chmod +x /etc/init.d/zabbix_ agentd
#ln -s /usr/local/zabbix/sbin/* /usr/local/sbin/
#ln -s /usr/local/zabbix/bin/* /usr/local/bin/
#vi /etc/rc.d/init.d/zabbix_ agentd
#
#chkconfig: 2345 10 90
#description: zabbix agent
#chkconfig --add zabbix_agentd
#chkconfig zabbix_agentd on
#service restart zabbix_ agentd