报错信息:
root@zabbix:/etc/zabbix/zabbix_agentd.d# zabbix_get -s 127.0.0.1 -p 10050 -k host.icmp_status[icmp_res_time,114.114.114.114]
Traceback (most recent call last):
File "/etc/zabbix/zabbix_agentd.d/custom_script.py", line 523, in <module>
items[params[1]](params)
File "/etc/zabbix/zabbix_agentd.d/custom_script.py", line 482, in icmp
Sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp"))
File "/usr/lib/python3.8/socket.py", line 231, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
PermissionError: [Errno 1] Operation not permitted
原始代码:
def icmp(*args):
values = args[0]
def incksum(packet):
if len(packet) & 1:
packet = packet + '\0'
words = array.array('h', packet)
sum = 0
for word in words:
sum += (word & 0xffff)
sum = (sum >> 16) + (sum & 0xffff)
sum = sum + (sum >> 16)
return (~sum) & 0xffff
header = struct.pack('bbHHh', 8, 0, 0, 12345, 0) # 创建头部
data = struct.pack('d', time.time()) # 创建data数据 时间戳随机
packet = header + data
chksum = incksum(packet)
header = struct.pack('bbHHh', 8, 0, chksum, 12345, 0)
data = header + data
Sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp"))
Sock.settimeout(0.5)
Sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
results = []
if values[1] == 'icmp_status':
for value in range(int(values[3])):
try:
Sock.sendto(data, (values[2], 0))
recv_packet, addr = Sock.recvfrom(1024)
type_value, code, checksum, packet_id, sequence, = struct.unpack("bbHHh",
recv_packet[
20:28]) # 前20个字节为ip头部信息,后8位为icmp头部信息
if type_value == 0 and code == 0:
results.append(1)
break
else:
results.append(0)
except socket.timeout:
results.append(0)
if 1 in results:
print(1)
else:
print(0)
elif values[1] == 'icmp_res_time':
send_time = time.time()
try:
Sock.sendto(data, (values[2], 0))
recv_packet, addr = Sock.recvfrom(1024)
rec_time = time.time()
response_time = (rec_time - send_time) * 1000
print(response_time)
except socket.timeout:
rec_time = time.time()
response_time = (rec_time - send_time) * 1000
print(response_time)
相似问题