Module: check_mk
Branch: master
Commit: b020459e54b25113faaf4a74bcfcf2e526ec55c1
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=b020459e54b251…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Mon Mar 12 14:40:19 2012 +0100
New ValueSpec for IPv4 Network
---
web/htdocs/valuespec.py | 43 +++++++++++++++++++++++++++++++++++++++++++
1 files changed, 43 insertions(+), 0 deletions(-)
diff --git a/web/htdocs/valuespec.py b/web/htdocs/valuespec.py
index d43887e..5bd8b5b 100644
--- a/web/htdocs/valuespec.py
+++ b/web/htdocs/valuespec.py
@@ -328,6 +328,49 @@ class EmailAddress(TextAscii):
def value_to_text(self, value):
return '<a href="mailto:%s">%s</a>' % (value,
value)
+
+# Network as used in routing configuration, such as
+# "10.0.0.0/8" or "192.168.56.1"
+class IPv4Network(TextAscii):
+ def __init__(self, **kwargs):
+ kwargs.setdefault("size", 12)
+ TextAscii.__init__(self, **kwargs)
+
+ def validate_value(self, value, varprefix):
+ if "/" in value:
+ try:
+ network, bits = value.split("/")
+ bits = int(bits)
+ except:
+ raise MKUserError(varprefix, _("Please use the syntax
X.X.X.X/YY"))
+ else:
+ network = value
+ bits = 32
+
+ if bits < 0 or bits > 32:
+ raise MKUserError(varprefix, _("Invalid number of bits. Must be in range
1 ... 32"))
+
+ if value.count(".") != 3:
+ raise MKUserError(varprefix, _("The network must contain three
dots"))
+ try:
+ octets = map(int, network.split("."))
+ if len(octets) != 4:
+ raise MKUserError(varprefix, _("Please specify four octets
(X.X.X.X/YY)"))
+ for o in octets:
+ if o < 0 or o > 255:
+ raise MKUserError(varprefix, _("Invalid octet %d (must be in
range 1 ... 255)") % o)
+ except MKUserError:
+ raise
+ except:
+ raise MKUserError(varprefix, _("Please use the syntax
X.X.X.X/YY"))
+
+ # Make sure that non-network bits are zero
+ l = (octets[0] << 24) + (octets[1] << 16) + (octets[2] << 8) +
(octets[3])
+ for b in range(bits, 32):
+ if l & (2 ** (31-b)) != 0:
+ raise MKUserError(varprefix, _("Please make sure that only the %d
non-network bits are non-zero") % bits)
+
+
# Valuespec for a HTTP Url (not HTTPS), that
# automatically adds http:// to the value
class HTTPUrl(TextAscii):