Skip to content

Commit

Permalink
chore: updated alertmanager to api v2
Browse files Browse the repository at this point in the history
  • Loading branch information
amunchet committed Nov 7, 2024
1 parent a76e940 commit 3edc4fd
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion alertmanager/test_alert.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh
url='http://localhost:9093/api/v1/alerts'
url='http://localhost:9093/api/v2/alerts'
name="TEST ALERT"
echo "firing up alert $name"

Expand Down
2 changes: 1 addition & 1 deletion alertmanager/test_resolve.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh
url='http://localhost:9093/api/v1/alerts'
url='http://localhost:9093/api/v2/alerts'
name="TEST ALERT"
echo "resolving alert $name"

Expand Down
2 changes: 1 addition & 1 deletion backend/alive.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def check_port(host, port):
return False


def check_all_hosts():
def check_all_hosts(): # pragma: no cover
"""
Pulls in all hosts and checks them all
"""
Expand Down
23 changes: 17 additions & 6 deletions backend/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,11 @@ def list_alerts():
"""
url = "http://alertmanager:9093/api/v2/alerts"
password = open("/alertmanager/pass").read()
headers = {
"Content-Type" : "application/json"
}

return json.dumps(requests.get(url, auth=("admin", password)).json()), 200
return json.dumps(requests.get(url, auth=("admin", password), headers=headers).json()), 200


@app.route("/alertmanager/alert", methods=["POST"])
Expand All @@ -842,15 +845,19 @@ def resolve_alert(data=""):
else: # pragma: no cover
return "Invalid data", 419

url = "http://alertmanager:9093/api/v1/alerts"
url = "http://alertmanager:9093/api/v2/alerts"
password = open("/alertmanager/pass").read()

del parsed_data["startsAt"]
parsed_data["status"] = "resolved"
parsed_data["endsAt"] = "2021-08-03T14:34:41-05:00"

headers = {
"Content-Type" : "application/json"
}

retval = requests.post(
url, data=json.dumps([parsed_data]), auth=("admin", password)
url, data=json.dumps([parsed_data]), auth=("admin", password), headers=headers
)

return retval.text, retval.status_code
Expand All @@ -865,7 +872,11 @@ def restart_alertmanager():
url = "http://alertmanager:9093/-/reload"
password = open("/alertmanager/pass").read()

retval = requests.post(url, auth=("admin", password))
headers = {
"Content-Type" : "application/json"
}

retval = requests.post(url, auth=("admin", password), headers=headers)
return retval.text, retval.status_code


Expand Down Expand Up @@ -1192,7 +1203,7 @@ def save_ansible_file(fname, inp_data="", vars_file=""):
# Ansible runner
@app.route("/ansible_runner/", methods=["POST"])
@requires_auth_admin
def run_ansible(inp_data=""): # pragma: no cover
def run_ansible(inp_data=""):
if inp_data != "":
data = inp_data
elif request.method == "POST": # pragma: no cover
Expand Down Expand Up @@ -1228,7 +1239,7 @@ def run_ansible(inp_data=""): # pragma: no cover
cmdline="-vvvvv --vault-password-file ../vault.pass",
quiet=True
)
except Exception as e:
except Exception as e: # pragma: no cover
# Delete Vault Password
if "vault.pass" in os.listdir(RUN_DIR):

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
os.remove("{}/vault.pass".format(RUN_DIR))

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
Expand Down
5 changes: 4 additions & 1 deletion backend/test/test_01_alertmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,17 @@ def test_send_alert():
- Will just print out URL and payload
"""
a = unwrap(serve.list_alerts)()
print(a[0])
assert a[1] == 200
b = json.loads(a[0])

assert watcher.send_alert(
output= watcher.send_alert(
"test-alert",
"test-service",
"test-host",
)
print(output.text)
assert output
a = unwrap(serve.list_alerts)()
assert a[1] == 200

Expand Down
13 changes: 8 additions & 5 deletions backend/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,25 @@ def send_alert(
:param serverity - defaults to error
"""
url = "http://alertmanager:9093/api/v1/alerts"
url = "http://alertmanager:9093/api/v2/alerts"
headers = {
"Content-Type": "application/json"
}
data = {
"status": "firing",
"labels": {
"alertname": alert_name,
"service": service,
"severity": severity,
"instance": instance,
"instance": instance
},
"annotations": {
"summary": summary,
"summary": summary
},
"generatorURL": url,
"generatorURL": url
}

password = open("/alertmanager/pass").read()

retval = requests.post(url, data=json.dumps([data]), auth=("admin", password))
retval = requests.post(url, headers=headers, data=json.dumps([data]), auth=("admin", password))
return retval

0 comments on commit 3edc4fd

Please sign in to comment.