-
Notifications
You must be signed in to change notification settings - Fork 57
/
check_ceph_rgw_api
executable file
·116 lines (99 loc) · 4.23 KB
/
check_ceph_rgw_api
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2014 Catalyst IT http://www.catalyst.net.nz
# Copyright (c) 2015 SWITCH http://www.switch.ch
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# 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.
from __future__ import print_function
import requests
import warnings
import json
import argparse
import sys
from awsauth import S3Auth
__version__ = '1.7.2'
# nagios exit code
STATUS_OK = 0
STATUS_WARNING = 1
STATUS_CRITICAL = 2
STATUS_UNKNOWN = 3
def main():
# parse args
parser = argparse.ArgumentParser(description="'radosgw api bucket stats' nagios plugin.")
parser.add_argument('-H', '--host', help="Server URL for the radosgw api (example: http://objects.dreamhost.com/)", required=True)
parser.add_argument('-k', '--insecure', help="Allow insecure server connections when using SSL", action="store_false")
parser.add_argument('-e', '--admin_entry', help="The entry point for an admin request URL [default is '%(default)s']", default="admin")
parser.add_argument('-a', '--access_key', help="S3 access key", required=True)
parser.add_argument('-s', '--secret_key', help="S3 secret key", required=True)
parser.add_argument('-d', '--detail', help="output perf data for all buckets", action="store_true")
parser.add_argument('-b', '--byte', help="output perf data in Byte instead of KB", action="store_true")
parser.add_argument('-v', '--version', help='show version and exit', action="store_true")
args = parser.parse_args()
if args.version:
print("version {0}".format(__version__))
return STATUS_OK
# helpers for default schema
if not args.host.startswith("http"):
args.host = "http://{0}".format(args.host)
# and for request_uri
if not args.host.endswith("/"):
args.host = "{0}/".format(args.host)
url = "{0}{1}/bucket?format=json&stats=True".format(args.host,
args.admin_entry)
try:
# Inversion of condition, when '--insecure' is defined we disable
# requests warning about certificate hostname mismatch.
if not args.insecure:
warnings.filterwarnings('ignore', message='Unverified HTTPS request')
response = requests.get(url, verify=args.insecure,
auth=S3Auth(args.access_key, args.secret_key,
args.host))
if response.status_code == requests.codes.ok:
bucket_stats = response.json()
else:
# no usage caps or wrong admin entry
print("RGW ERROR [{0}]: {1}".format(response.status_code,
response.content.decode('utf-8')))
return STATUS_WARNING
# DNS, connection errors, etc
except requests.exceptions.RequestException as e:
print("RGW ERROR: {0}".format(e))
return STATUS_UNKNOWN
#print(bucket_stats)
buckets = []
for i in bucket_stats:
if type(i) is dict:
bucket_name = i['bucket']
usage_dict = i['usage']
if usage_dict and 'rgw.main' in usage_dict:
bucket_usage_kb = usage_dict['rgw.main']['size_kb_actual']
else:
bucket_usage_kb = 0
buckets.append((bucket_name, bucket_usage_kb))
buckets_total_kb = sum([b[1] for b in buckets])
status = "RGW OK: {0} buckets, {1} KB total | /={2}{3} "
if args.byte:
status = status.format(len(buckets), buckets_total_kb, buckets_total_kb*1024, "B")
else:
status = status.format(len(buckets), buckets_total_kb, buckets_total_kb, "KB")
#print(buckets)
if buckets and args.detail:
if args.byte:
status = status + " ".join(["{}={}B".format(b[0], b[1]*1024) for b in buckets])
else:
status = status + " ".join(["{}={}KB".format(b[0], b[1]) for b in buckets])
print(status)
return STATUS_OK
if __name__ == "__main__":
sys.exit(main())