Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ Copy `config.json.example` to `config.json`, then edit:
- `tasks`: Websites you wanna monitor
- `Category`: `Reference` or _Whatever you like_
- `Name`: The system name in your [cState config.yml](https://github.com/cstate/example/blob/master/config.yml)
- `URL`: The HTTP GET url you want to monitor
- `Code`: The expecting status code
- `URL`: The HTTP GET url you want to monitor *or* the hostname or IP address to check
- `Code`: The expecting status code *or* the port you want to check
- `Scheme`: Either HTTP or TCP. Note that the TCP scheme expects `URL` to be an IP or hostname, and `Code` to be a port. View the `congfig.json.example` for more info

## What's `Reference` for?

Expand Down
22 changes: 13 additions & 9 deletions config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,32 @@
"repo": "thuhole/status",
"tasks": [
{
"Category": "Main",
"Name": "API",
"URL": "https://thuhole.com/",
"Code": 200
"Category": "Main Services",
"Name": "FTP",
"URL": "thuhole.com",
"Code": 21,
"Scheme": "tcp",
},
{
"Category": "Main",
"Category": "Main Website",
"Name": "Images",
"URL": "https://img.thuhole.com",
"Code": 403
"Code": 403,
"Scheme": "http"
},
{
"Category": "Reference",
"Name": "Zhihu",
"URL": "https://www.zhihu.com",
"Code": 302
"Code": 302,
"Scheme": "http"
},
{
"Category": "Reference",
"Name": "Google",
"URL": "https://www.google.com/gen_204",
"Code": 204
}
"Code": 204,
"Scheme": "http"
},
]
}
41 changes: 26 additions & 15 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from enum import Enum

import requests
import socket
from github import Github

REFRESH_INTERVAL = 60
Expand Down Expand Up @@ -53,7 +54,7 @@ def publishOffline(self):
try:
content = """---
section: issue
title: Disruption Detected
title: Disruption Detected - {}
date: {}
resolved: false
informational: false
Expand All @@ -62,10 +63,10 @@ def publishOffline(self):
- {}
severity: disrupted
---
*Investigating* - We are investigating a potential issue that might affect the uptime of one our of services. We are sorry for any inconvenience this may cause you. This incident post will be updated once we have more information.
*Investigating* - We are investigating a potential issue that might affect the uptime of one of our services. We are sorry for any inconveniences this may cause you. This incident post will be updated once we have more information.

This is an automatic post by a monitor bot.
""".format(self.date, self.name)
""".format(self.name, self.date, self.name)
filename = self.date + ".md"
r = repo.create_file("content/issues/" + filename,
"create " + filename, content)
Expand Down Expand Up @@ -97,19 +98,29 @@ def publishOnline(self):
return e


def checkConnection(url, code):
try:
r = requests.get(url, headers={"User-Agent": "cState_Probe/0.0.1"}, timeout=TIMEOUT, allow_redirects=False)
logger.debug(str(r.elapsed.total_seconds()) + "s elapsed to get " + url)
if r.status_code == code:
def checkConnection(url, code, scheme):
if scheme.upper() == "HTTP":
try:
r = requests.get(url, headers={"User-Agent": "cState_Probe/0.0.1"}, timeout=TIMEOUT, allow_redirects=False)
logger.debug(str(r.elapsed.total_seconds()) + "s elapsed to get " + url)
if r.status_code == code:
return True
else:
logger.warning("inconsistent status code for " + url +
", expecting " + str(code) + ", got " + str(r.status_code))
return False
except Exception as e:
logger.warning('failed to get ' + url + ', err=' + str(e))
return False
elif scheme.upper() == "TCP":
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((url, int(code)))
s.shutdown(2)
return True
else:
logger.warning("inconsistent status code for " + url +
", expecting " + str(code) + ", got " + str(r.status_code))
except Exception as e:
logger.warning(f'failed to connect to socket {url}:{code} err={e}')
return False
except Exception as e:
logger.warning("failed to get " + url + ", err=" + str(e))
return False


class ProducerThread(threading.Thread):
Expand All @@ -119,7 +130,7 @@ def __init__(self):
def run(self):
while True:
for task in tasks:
task["now_success"] = checkConnection(task["URL"], task["Code"])
task["now_success"] = checkConnection(task["URL"], task["Code"], task["Scheme"])

reference_success = all([x["now_success"] for x in tasks if x["Category"] == "Reference"])
for task in tasks:
Expand Down