XyloSiteMonitor

Check-in [7f4091db1d]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:added a flag for email only on fail and a flag for giving the run a name (annotation)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7f4091db1d5fde6b73302e49129b092b8baa55a7d60dbf60e98939f91fc08a64
User & Date: xylon 2018-11-04 15:35:09
Context
2018-11-04
15:48
Improve comments check-in: af09294ed6 user: xylon tags: trunk
15:35
added a flag for email only on fail and a flag for giving the run a name (annotation) check-in: 7f4091db1d user: xylon tags: trunk
13:30
Added script and example definitions check-in: 98b51cc783 user: xylon tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to xylositemonitor.py.

9
10
11
12
13
14
15


16
17
18


19
20
21
22
23
24
25
..
29
30
31
32
33
34
35









36
37
38
39
40
41
42
..
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
...
309
310
311
312
313
314
315

316
317
318
319
320
321
import yaml
import pycurl

# Command line arguments.
parser = argparse.ArgumentParser(description='Tests websites.')
parser.add_argument('--sites-file', dest='sitesfile')
parser.add_argument('--mailto', dest='mailaddress')


args = parser.parse_args()
mailto = args.mailaddress
sitesfile = args.sitesfile



# We need to test both that it's not None and that it's not empty
try:
    re.search('[a-zA-Z0-9]+', mailto).group(0)  # this will error on
                                                # both blank string
                                                # and non-string
except (TypeError, AttributeError):
................................................................................
try:
    re.search('[a-zA-Z0-9]+', sitesfile).group(0)  # this will error
                                                   # on both blank
                                                   # string and
                                                   # non-string
except (TypeError, AttributeError):
    sitesfile = "/etc/xylosites.yml"










# don't even try to open sitesfile unless it's there
if not os.path.isfile(sitesfile):
    print('Initialisation Error! Cannot find sitesfile at "' + sitesfile +
          '"\nPlease place it here or specify location with --sites-file=')
    sys.exit()

................................................................................
    sites = yaml.load(stream)

def send_mail(subject):
    import smtplib
    from email.mime.text import MIMEText

    msg = MIMEText(mail_body)
    msg['Subject'] = 'XyloSiteMonitor: ' + subject
    msg['To'] = mailto
    msg['From'] = "xylositemonitor"

    smtpcon = smtplib.SMTP('localhost')  # use host as mail relay
    smtpcon.send_message(msg)
    smtpcon.quit()  # close connection

................................................................................
    mail_body += str(success_count) + " tests passed\n"
    mail_body += str(fail_count) + " tests failed\n"

    # OK so we've got our mail body, now we just need to work out what our subject is
    if fail_count > 0:
        send_mail(str(fail_count) + ' failing tests!')
    else:

        send_mail("all " + str(success_count) + "tests passed")

print("")
print("Summary:")
print(str(success_count) + " tests passed")
print(str(fail_count) + " tests failed")







>
>



>
>







 







>
>
>
>
>
>
>
>
>







 







|







 







>
|





9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
..
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
..
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
...
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import yaml
import pycurl

# Command line arguments.
parser = argparse.ArgumentParser(description='Tests websites.')
parser.add_argument('--sites-file', dest='sitesfile')
parser.add_argument('--mailto', dest='mailaddress')
parser.add_argument('--annotation', dest='annotation')
parser.add_argument('--email-only-on-fail', dest='emailonlyfail', action='store_true')
args = parser.parse_args()
mailto = args.mailaddress
sitesfile = args.sitesfile
annotation = args.annotation
emailonlyfail = args.emailonlyfail

# We need to test both that it's not None and that it's not empty
try:
    re.search('[a-zA-Z0-9]+', mailto).group(0)  # this will error on
                                                # both blank string
                                                # and non-string
except (TypeError, AttributeError):
................................................................................
try:
    re.search('[a-zA-Z0-9]+', sitesfile).group(0)  # this will error
                                                   # on both blank
                                                   # string and
                                                   # non-string
except (TypeError, AttributeError):
    sitesfile = "/etc/xylosites.yml"

# We need to test both that it's not None and that it's not empty
try:
    re.search('[a-zA-Z0-9]+', annotation).group(0)  # this will error
                                                    # on both blank
                                                    # string and
                                                    # non-string
except (TypeError, AttributeError):
    annotation = "XyloSiteMonitor"

# don't even try to open sitesfile unless it's there
if not os.path.isfile(sitesfile):
    print('Initialisation Error! Cannot find sitesfile at "' + sitesfile +
          '"\nPlease place it here or specify location with --sites-file=')
    sys.exit()

................................................................................
    sites = yaml.load(stream)

def send_mail(subject):
    import smtplib
    from email.mime.text import MIMEText

    msg = MIMEText(mail_body)
    msg['Subject'] = annotation + ': ' + subject
    msg['To'] = mailto
    msg['From'] = "xylositemonitor"

    smtpcon = smtplib.SMTP('localhost')  # use host as mail relay
    smtpcon.send_message(msg)
    smtpcon.quit()  # close connection

................................................................................
    mail_body += str(success_count) + " tests passed\n"
    mail_body += str(fail_count) + " tests failed\n"

    # OK so we've got our mail body, now we just need to work out what our subject is
    if fail_count > 0:
        send_mail(str(fail_count) + ' failing tests!')
    else:
        if not emailonlyfail:
            send_mail("all " + str(success_count) + "tests passed")

print("")
print("Summary:")
print(str(success_count) + " tests passed")
print(str(fail_count) + " tests failed")