-
Notifications
You must be signed in to change notification settings - Fork 1
/
librato-imap
executable file
·95 lines (74 loc) · 3.4 KB
/
librato-imap
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
#!/usr/bin/env python
#
# Author: Ian Gable <[email protected]>
#
# Check the number of unread email in an IMAP inbox
# and pushes that number to Librato Metrics
#
import getpass
import imaplib
import sys
import time
from imaplib import IMAP4
from librato import LibratoConnection
from librato.exceptions import NotFound
from optparse import OptionParser
def pushmessages(username, password, imap_server,librato_user,api_key, bequiet):
try:
imap = imaplib.IMAP4_SSL(imap_server,'993')
imap.login(username,password)
# Default to looking at the inbox for now
imap.select('INBOX')
typ, data = imap.search(None, 'UNSEEN')
num_messages = str(len(data[0].split()))
if not bequiet:
print "Unread Messages:" + str(len(data[0].split()))
imap.close()
imap.logout()
except IMAP4.error as e:
print str(e)
connection = LibratoConnection(librato_user,api_key)
try:
# line below uses the https://github.com/millarm/librato fork syntax (not in PyPI)
# gauge = connection.get_or_create_gauge('unread_email',description="Inbox Unread Email")
gauge = connection.get_gauge('unread_email')
except NotFound:
gauge = connection.create_gauge('unread_email',description='Unread Email')
# line below uses the https://github.com/millarm/librato fork syntax (not in PyPI)
#gauge = connection.get_or_create_gauge('unread_email',description="Inbox Unread Email")
gauge.add(num_messages)
if __name__=='__main__':
parser = OptionParser(usage="%prog -k INFILE -o OUTFILE",version = "%prog 0.1")
parser.add_option("-u", "--username", dest="username",
help="IMAP username", metavar="USERNAME")
parser.add_option("-l","--librato-user", dest="librato_user",
help="Librato username, usually an email address", metavar="LIBRATOUSER")
parser.add_option("-k", "--api-key", dest="api_key",
help="Librato API Key", metavar="APIKEY")
parser.add_option("-i","--imap-server",dest="imap_server",
help="IMAP server to check", metavar="IMAPSERVER")
parser.add_option("-p","--polling-frequency",dest="polling",
help="The time in seconds between polling the IMAP server",
metavar="POLLINGFREQUENCY", default=600)
parser.add_option("-q","--quiet", action="store_true", dest="quiet",
help="Set no output to the STDOUT", default=False)
(options, args) = parser.parse_args()
missing_arguments = False
if not options.username and options.imap_server and options.librato_user and options.api_key:
print("You are missing some required arguments\n")
parser.print_help()
parser.error("Missing some arguments so exiting")
missing_options = True
password = getpass.getpass("IMAP Password:")
username = options.username
imap_server = options.imap_server
librato_user = options.librato_user
api_key = options.api_key
polling_freq = float(options.polling)
bequiet = options.quiet
try:
while True:
pushmessages(username,password, imap_server,librato_user,api_key, bequiet)
time.sleep(polling_freq)
except (SystemExit, KeyboardInterrupt):
sys.exit()