When I listened to the seminar "Fully Automatic Zabbix" at OSC2015 Tokyo / Spring and registered NetArc Discovery in a wide range, it took time to search, so I heard that it is better to register separately ... But, "It's troublesome to calculate the range and divide it" ... So, after studying Python, I thought about the logic to divide / 22 or / 23 into 24 units by specifying the IP range.
It is the logic to divide the IP. Since this is a logic test, variable names are appropriate. (excuse) It's messy-A (^_^;
import sys
io = sys.argv[1]
tmp = io.split("/")
ip = tmp[0]
prefix = tmp[1]
tmp = ip.split(".")
o1 = tmp[0]
o2 = tmp[1]
o3 = tmp[2]
o4 = tmp[3]
sum = (int(o1) << 24 | int(o2) << 16 | int(o3) << 8 | int(o4) ) & 0xFFFFFF00
sum = sum >> (32-int(prefix))
sum = sum << (32-int(prefix))
for i in range(0, 2 << (24 - int(prefix) - 1)):
a = sum | i<<8
o1 = (a & 0xFF000000) >> 24
o2 = (a & 0x00FF0000) >> 16
o3 = (a & 0x0000FF00) >> 8
o4 = 0
a = str(o1) + "." + str(o2) + "." + str(o3) + "." + str(o4) + "/24"
print a
Execution result
Python on Windows ... A (^_^;
After that, you can register the divided IP using Zabbix API ...
Running the Zabbix API in Python is easy with the article below. [Try the ZABBIX API with Pyhton](http://www.zumwalt.info/blog/2012/11/pyhton%E3%81%A7zabbix-api%E3%82%92%E8%A7%A6%E3 % 81% A3% E3% 81% A6% E3% 81% BF% E3% 82% 8B /)
As mentioned in the article, I also used pycurl at first, but switched to urllib2. A (^_^;
When using Zabbix API in Python, it is convenient to create xxx.json for request. If you read the file with json.load, replace auth of params and various parameters and throw it, you do not have to write all the requests in the program as an excuse, which is convenient.
Recommended Posts