Merge pull request #9 from invertigo/master

add arguments for http timeout and tcp port
This commit is contained in:
Drew Kerrigan 2015-05-07 19:48:51 -04:00
commit 1772543ee3
2 changed files with 11 additions and 2 deletions

View File

@ -62,6 +62,7 @@ service
optional arguments:
-h, --help show this help message and exit
-H HOST, --host HOST Host.
-H PORT, --port PORT TCP port.
-B AUTH, --basic-auth AUTH
Basic auth string "username:password"
-p PATH, --path PATH Path.
@ -88,6 +89,8 @@ optional arguments:
this parameter are: (key), (key,UnitOfMeasure),
(key,UnitOfMeasure,Min,Max).
-s, --ssl HTTPS mode.
-t TIMEOUT, --timeout TIMEOUT
Connection timeout (seconds)
-f SEPARATOR, --field_separator SEPARATOR
Json Field separator, defaults to "." ; Select element
in an array with "(" ")"
@ -265,4 +268,4 @@ In this example I've chosen `_` to separate `guages` from `jvm` and `capacity` f
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.

View File

@ -187,6 +187,7 @@ def parseArgs():
and determines the status and performance data for that service')
parser.add_argument('-H', '--host', dest='host', required=True, help='Host.')
parser.add_argument('-P', '--port', dest='port', required=False, help='TCP port')
parser.add_argument('-B', '--basic-auth', dest='auth', required=False, help='Basic auth string "username:password"')
parser.add_argument('-p', '--path', dest='path', help='Path.')
parser.add_argument('-e', '--key_exists', dest='key_list', nargs='*',
@ -204,6 +205,7 @@ def parseArgs():
More information about Range format and units of measure for nagios can be found at https://nagios-plugins.org/doc/guidelines.html\
Additional formats for this parameter are: (key), (key,UnitOfMeasure), (key,UnitOfMeasure,Min,Max).')
parser.add_argument('-s', '--ssl', action='store_true', help='HTTPS mode.')
parser.add_argument('-t', '--timeout', type=int, help='Connection timeout (seconds)')
parser.add_argument('-f', '--field_separator', dest='separator', help='Json Field separator, defaults to "." ; Select element in an array with "(" ")"')
parser.add_argument('-d', '--debug', action='store_true', help='Debug mode.')
@ -226,6 +228,7 @@ if __name__ == "__main__":
else:
url = "http://%s" % args.host
if args.port: url += ":%s" % args.port
if args.path: url += "/%s" % args.path
debugPrint(args.debug, "url:%s" % url)
@ -235,7 +238,10 @@ if __name__ == "__main__":
if args.auth:
base64str = base64.encodestring(args.auth).replace('\n', '')
req.add_header('Authorization', 'Basic %s' % base64str)
response = urllib2.urlopen(req)
if args.timeout:
response = urllib2.urlopen(req, timeout=args.timeout)
else:
response = urllib2.urlopen(req)
except HTTPError as e:
nagios.unknown("HTTPError[%s], url:%s" % (str(e.code), url))
except URLError as e: