Python script giám sát hệ thống tích hợp với Nagios
Hello Devopers.
Mình mới viết một script nhỏ để giám sát hệ thống sensor bằng python3.
Mục đính là để giám sát hệ thống bật tắt một thiết bị. Trong script này thiết bị phải cung cấp gía trị giám sát được truyền theo định dạng file : xml.
Ví dụ:
Trong script này mình sử dụng hàm xml và urllib.
Chúng ta tạo một file export.xml và đọc nội dung file đó. Khi hệ thống Nagios quét tuần tự thì thông số trong file export.xml được cập nhật.
AlarmState : Có giá trị 0 hoặc 1 tùy theo cảnh báo.
Nagios làm việc thông qua Exit code. Trong ví dụ trên : Exit code == 2 khi hệ thống Critical và Exit code == 0 khi hệ thống bình thường.
Mình mới viết một script nhỏ để giám sát hệ thống sensor bằng python3.
Mục đính là để giám sát hệ thống bật tắt một thiết bị. Trong script này thiết bị phải cung cấp gía trị giám sát được truyền theo định dạng file : xml.
Ví dụ:
url = "http://192.168.10.200:8880/values.xml"
Trong script này mình sử dụng hàm xml và urllib.
#!/usr/bin/env python3
import sys
import ssl
from lxml import etree
import urllib3
import urllib.request
import xml.etree.ElementTree as xee
context = ssl._create_unverified_context()
url = "http://192.168.10.200:8880/values.xml"
s = urllib.request.urlopen(url, context = context)
contents = s.read()
file = open("export.xml", 'wb')
file.write(contents)
file.close()
Chúng ta tạo một file export.xml và đọc nội dung file đó. Khi hệ thống Nagios quét tuần tự thì thông số trong file export.xml được cập nhật.
data = "export.xml"
web = etree.parse(data)
root = web.getroot()
for BinaryInSet in root:
for thutu in BinaryInSet:
ID = thutu.find('ID')
Value = thutu.find('Value')
AlarmState = thutu.find('AlarmState')
Name = thutu.find('Name')
if ID is not None and ID .text == '1':
if AlarmState.text == '1':
print ("Digital Input:",Name.text,",Value:", Value.text,"State:", "Critical",",AlarmState:", AlarmState.text, "Opened")
print ("CRITICAL - He thong canh bao")
sys.exit(2)
else:
print ("Digital Input:",Name.text,",Value:", Value.text,"State:", "Normal",",AlarmState:", AlarmState.text, "Closed")
print ("OK - He thong bao dam.")
sys.exit(0)
AlarmState : Có giá trị 0 hoặc 1 tùy theo cảnh báo.
Nagios làm việc thông qua Exit code. Trong ví dụ trên : Exit code == 2 khi hệ thống Critical và Exit code == 0 khi hệ thống bình thường.
Nhận xét
Đăng nhận xét