|
1 | 1 | #!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
2 | 4 | """ |
3 | | -Test example to take a ping and save it into an sqlite3 database |
4 | | -https://docs.python.org/3/library/sqlite3.html |
| 5 | +Is the website alive? This could be tested at a regular interval by retrieving |
| 6 | +a url and checking its status. These tests can be stored in a database which makes |
| 7 | +it easy to accumulate statistics and to query for various patterns. |
| 8 | +
|
| 9 | +Manual: |
| 10 | + https://docs.python.org/3/library/sqlite3.html |
| 11 | +
|
| 12 | +The database is named "ping.db" and is stored in the current directory. |
| 13 | +
|
| 14 | +If you have the command "sqlite3" installed you could use that to connect to the |
| 15 | +database and inspect it. |
| 16 | +
|
| 17 | +$ sqlite3 ping.db |
| 18 | +
|
5 | 19 | """ |
6 | | -# pip install requests |
| 20 | + |
7 | 21 | import requests |
8 | 22 | import sqlite3 |
9 | 23 | import time |
10 | 24 | import sys |
11 | 25 |
|
| 26 | + |
| 27 | + |
| 28 | +def isSQLite3(filename): |
| 29 | + """ |
| 30 | + Check if a file is a SQLite3 database. |
| 31 | + http://stackoverflow.com/questions/12932607/how-to-check-with-python-and-sqlite3-if-one-sqlite-database-file-exists |
| 32 | + """ |
| 33 | + from os.path import isfile, getsize |
| 34 | + |
| 35 | + if not isfile(filename): |
| 36 | + return False |
| 37 | + if getsize(filename) < 100: # SQLite database file header is 100 bytes |
| 38 | + return False |
| 39 | + else: |
| 40 | + fd = open(filename, 'rb') |
| 41 | + Header = fd.read(100) |
| 42 | + fd.close() |
| 43 | + |
| 44 | + if Header[0:16] == b'SQLite format 3\000': |
| 45 | + return True |
| 46 | + else: |
| 47 | + return False |
| 48 | + |
| 49 | + |
| 50 | +# |
12 | 51 | # Get argument if there is one and use that as url |
| 52 | +# |
13 | 53 | if len(sys.argv) == 2: |
14 | 54 | url = sys.argv[1] |
15 | 55 | else: |
16 | | - url = "http://dbwebb.se/" |
| 56 | + sys.stderr.write("Add the url (for example http://bth.se) to check as a command line argument.\n") |
| 57 | + sys.stderr.write("{command} http://bth.se.\n\n".format(command=sys.argv[0])) |
| 58 | + sys.exit(1) |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +print(__doc__) |
| 63 | +_ = input("Press enter to continue.") |
17 | 64 |
|
18 | | -# connect to the database and set a cursor |
19 | | -conn = sqlite3.connect("ping.sqlite3") |
20 | | -cur = conn.cursor() |
21 | 65 |
|
22 | | -# init database incase it's lost |
23 | | -cur.execute(""" |
24 | | -CREATE TABLE IF NOT EXISTS Pings |
25 | | -(url TEXT, time TEXT PRIMARY KEY, repsonse INTEGER)""") |
26 | 66 |
|
| 67 | +# |
| 68 | +# Connect to the database and set a cursor |
| 69 | +# |
| 70 | +dbFile = "ping.db" |
| 71 | + |
| 72 | +db = sqlite3.connect(dbFile) |
| 73 | +dbc = db.cursor() |
| 74 | + |
| 75 | + |
| 76 | +# |
| 77 | +# Init a new database if needed |
| 78 | +# |
| 79 | +dbc.execute("CREATE TABLE IF NOT EXISTS Pings (url TEXT, time TEXT, repsonse INTEGER)") |
| 80 | + |
| 81 | + |
| 82 | +# |
| 83 | +# Check if the database is valid |
| 84 | +# |
| 85 | +if not isSQLite3(dbFile): |
| 86 | + sys.stderr.write("The file '{}' is not a valid SQLite database.\n".format(dbFile)) |
| 87 | + sys.exit(1) |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +# |
27 | 92 | # get current time for logging |
| 93 | +# |
28 | 94 | rTime = time.ctime(time.time()) |
29 | 95 |
|
30 | 96 | try: |
31 | 97 | # url must start with "http://" or the request fails |
| 98 | + print("Connecting to: {}".format(url)) |
32 | 99 | r = requests.head(url) |
33 | 100 | code = r.status_code |
| 101 | + |
34 | 102 | except requests.ConnectionError: |
35 | 103 | code = 0 |
36 | 104 |
|
37 | | -# create the values to be added as tuple |
38 | | -values = (url, rTime, code) |
| 105 | + |
| 106 | + |
| 107 | +# |
39 | 108 | # insert values into database |
40 | | -cur.execute(""" |
41 | | -INSERT INTO Pings (url, time, repsonse) |
42 | | -VALUES (?, ?, ?) |
43 | | -""", values) |
44 | | - |
45 | | -print("Rows in db:") |
46 | | -# get all the rows in database |
47 | | -cur.execute("SELECT url, time, repsonse FROM Pings") |
48 | | -# loop through the result |
49 | | -for row in cur: |
| 109 | +# |
| 110 | +print("Storing log entry in database") |
| 111 | +dbc.execute("INSERT INTO Pings (url, time, repsonse) VALUES (?, ?, ?)", (url, rTime, code)) |
| 112 | + |
| 113 | +print("Retrieving all rows in database:") |
| 114 | +dbc.execute("SELECT url, time, repsonse FROM Pings") |
| 115 | + |
| 116 | +# Loop through a resultset from a SELECT query |
| 117 | +for row in dbc: |
50 | 118 | print(row) |
51 | 119 |
|
52 | | -# commit the changes and close the connection |
53 | | -conn.commit() |
54 | | -conn.close() |
| 120 | + |
| 121 | + |
| 122 | +# |
| 123 | +# Commit the changes and close the connection |
| 124 | +# |
| 125 | +db.commit() |
| 126 | +db.close() |
0 commit comments