Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to generate aggregate availability based on data from Signal Sciences.
#!/usr/bin/env python
"""
Script to generate aggregate availability based on data from Signal Sciences.
https://landing.google.com/sre/book/chapters/embracing-risk.html#risk-management_measuring-service-risk_aggregate-availability-equation
Usage:
./sigsci_site_availability.py -1d
Examples specifying different time periods:
./sigsci_site_availability.py -3d
./sigsci_site_availability.py -5d
./sigsci_site_availability.py -7d
"""
from __future__ import print_function
from __future__ import division
import os
import sys
from pysigsci import sigsciapi
FROM = "-1d"
if len(sys.argv) > 1:
FROM = sys.argv[1]
if "SIGSCI_EMAIL" in os.environ:
EMAIL = os.environ['SIGSCI_EMAIL']
else:
print('SIGSCI_EMAIL required.')
sys.exit()
if "SIGSCI_PASSWORD" in os.environ:
PASSWORD = os.environ['SIGSCI_PASSWORD']
else:
print('SIGSCI_PASSWORD required.')
sys.exit()
def main():
"""
Example main function
"""
# create sigsci api object
sigsci = sigsciapi.SigSciApi()
if "SIGSCI_CORP" in os.environ:
sigsci.corp = os.environ['SIGSCI_CORP']
else:
print('SIGSCI_CORP required.')
sys.exit()
if sigsci.auth(EMAIL, PASSWORD):
params = {"from": FROM}
# get overview report data
report = sigsci.get_overview_report(params)['data']
for site in report:
sigsci.site = site['name']
# get 5XX, 500, and 503 code count
server_errors = 0
params = {"from": FROM, "tag": "HTTP5XX"}
timeseries = sigsci.get_timeseries_requests(params)['data']
server_errors += timeseries[0]['summaryCount']
params = {"from": FROM, "tag": "HTTP500"}
timeseries = sigsci.get_timeseries_requests(params)['data']
server_errors += timeseries[0]['summaryCount']
params = {"from": FROM, "tag": "HTTP503"}
timeseries = sigsci.get_timeseries_requests(params)['data']
server_errors += timeseries[0]['summaryCount']
successful_requests = site['totalCount'] - server_errors
availability = 'No site activity.'
if site['totalCount'] > 0:
availability = '{:.2%}'.format(successful_requests / site['totalCount'])
print('{}:\n\tTotal Requests: {} Server Errors: {} Aggregate Availability: {}\n'.format(
site['displayName'],
site['totalCount'],
server_errors,
availability))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment