S3バケットの使用量を監視するnagiosプラグインをpythonで書きました。
pythonでnagiosプラグインを作成する時にとても便利な、pynagモジュールを利用しております。

■事前準備(モジュールインストール)

# yum install python-boto
# yum install pynag
# yum install python-yaml

■ソースコード

check_s3size.py

#!/usr/bin/env python
import boto,os,yaml
from pynag.Plugins import simple as Plugin
np = Plugin()
np.add_arg("f","key-file", "Enter a aws cridential key file", required=None)
np.add_arg("b","s3-bucket", "Enter a s3bucket name", required=None)
np.activate()

#parameter check
if np['critical'] < np['warning']:
        np.nagios_exit("UNKNOWN", "warning should be smaller than critical")

#connect s3bucket
if np['key-file']:
        key_file = np['key-file']

        #parameter check
        if not os.path.isfile(key_file):
                np.nagios_exit("UNKNOWN", "%s is not found" % key_file)
        credentials = yaml.load(file(key_file, 'r'))
        s3 = boto.connect_s3(credentials['accessKey'],credentials['secretKey'])
else:
        #IAM role should be attached
        s3 = boto.connect_s3()

S3Bucket_Size = 0

if np['s3-bucket']:
        #specified s3 bucket,only specified s3 bucket will be culculated
        bucket = s3.lookup(np['s3-bucket'])
        for key in bucket:
                S3Bucket_Size += key.size
else:
        #not specified s3 bucket,all s3 bucket will be culculated
        for bucket in s3.get_all_buckets():
                for key in bucket:
                        S3Bucket_Size += key.size

np.add_perfdata("S3Bucket_SIZE",S3Bucket_Size)
np.check_range(S3Bucket_Size)

■使用方法

# ./check_s3size.py -h
Usage: check_s3size.py [options]

Options:
    -h, --help                     show this help message and exit
    -f KEY-FILE, --key-file=KEY-FILE
                                        Enter a aws cridential key file
    -b S3-BUCKET, --s3-bucket=S3-BUCKET
                                        Enter a s3bucket name
    -v VERBOSE, --verbose=VERBOSE
                                        Verbosity Level
    -H HOST, --host=HOST Target Host
    -t TIMEOUT, --timeout=TIMEOUT
                                        Connection Timeout
    -c CRITICAL, --critical=CRITICAL
                                        Critical Threshhold
    -w WARNING, --warning=WARNING
                                        Warn Threshhold

■使用例

例1:awsアクセスキーファイルを指定する場合

# ./check_s3size.py -c 300000 -w 230000 -b koujipstest -f /root/credential.yaml
OK: data 221250 is inside warning=230000 and critical=300000 | 'S3Bucket_SIZE'=221250;;;;

例2:awsアクセスキーファイルを指定せず、IAM roleを利用する場合

# ./check_s3size.py -c 300000 -w 200000 -b koujipstest
WARNING: data 221250 is outside warning range: 200000 | 'S3Bucket_SIZE'=221250;;;;