From fe2e830bf767ba619f4b2ac03904bca3f30a03c0 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 7 May 2015 22:16:58 +0000 Subject: [PATCH] add arguments for http timeout and tcp port --- README.md | 5 ++++- check_http_json.py | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d5967cb..42508ee 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file + limitations under the License. diff --git a/check_http_json.py b/check_http_json.py index b22be00..bd064b3 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -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: