Skip to content

Instantly share code, notes, and snippets.

@rajathithan
Last active October 12, 2022 23:41
Show Gist options
  • Save rajathithan/c4a2e7afabdd7e78700f3b28e51bea5b to your computer and use it in GitHub Desktop.
Save rajathithan/c4a2e7afabdd7e78700f3b28e51bea5b to your computer and use it in GitHub Desktop.
Router down
# Author - Rajathithan Rajasekar
import base64
import json
from datetime import datetime
from google.cloud import storage
import requests
def process_log_entry(data, context):
data_buffer = base64.b64decode(data["data"])
log_entry = json.loads(data_buffer)
print(log_entry["textPayload"])
client = storage.Client()
bucket = client.get_bucket("xxxxxxx")
blob = bucket.get_blob("cloud-router-down.txt")
now = datetime.now()
dt = now.strftime("%Y-%m-%d %H:%M:%S")
previous_date = blob.download_as_string()
print(f"Last date and time - router is down - {previous_date}")
blob.upload_from_string(dt)
blob = bucket.get_blob("write-to-snow.txt")
cani = str(blob.download_as_string())
print(f"servicenow - write - status - {cani}")
if cani == "b'1'":
url = "https://demo.service-now.com/api/now/table/incident"
auth = ("username", "password")
data = {"caller_id" : "xxxxxxxxxxxxxxxxxxxxxxx",
"category" : "Fault",
"subcategory" : "Issues",
"cmdb_ci" : "xxxxxxxxxxxxxxxxxxxxxxx",
"short_description" : "google cloud testing",
"description" : "Testing Google-cloud service - Cloud router is down",
"assignment_group": "test group",
}
#data = json.dumps(data)
headers = {"Content-type" : "application/json", "Accept" : "application/json"}
r = requests.post(url, auth = auth, headers = headers, json = data)
scode = r.status_code
print(f"Status Code - {r.status_code}")
rjson = r.json()
print(f"Result - {rjson}")
blob = bucket.get_blob('incident.txt')
inc = rjson['result']['number']
sys_id = rjson['result']['sys_id']
inc = inc + " , https://demo.service-now.com/api/now/table/incident/" + sys_id
blob.upload_from_string(inc)
blob = bucket.get_blob("write-to-snow.txt")
nw = "0"
blob.upload_from_string(nw)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment