Révision 4fa3811c
Add optional preemptive HTTP Basic Authentication to HTTP request in order to
support monit configuration like "set httpd port 2812 allow munin:s3cr3t
read-only"
| plugins/monit/monit_parser | ||
|---|---|---|
| 14 | 14 |
|
| 15 | 15 |
set httpd port 2812 |
| 16 | 16 |
|
| 17 |
Optionally monit authentication can be configured, e.g.: |
|
| 17 | 18 |
|
| 19 |
set httpd port 2812 |
|
| 20 |
allow munin:s3cr3t read-only |
|
| 18 | 21 |
|
| 19 | 22 |
|
| 20 | 23 |
=head1 CONFIGURATION |
| ... | ... | |
| 25 | 28 |
env.port 2812 |
| 26 | 29 |
env.host localhost |
| 27 | 30 |
|
| 31 |
Optionally monit authentication can be configured, e.g.: |
|
| 32 |
|
|
| 33 |
[monit_parser] |
|
| 34 |
env.username munin |
|
| 35 |
env.password s3cr3t |
|
| 36 |
|
|
| 28 | 37 |
|
| 29 | 38 |
=head1 AUTHOR |
| 30 | 39 |
|
| ... | ... | |
| 44 | 53 |
import os |
| 45 | 54 |
import sys |
| 46 | 55 |
import urllib.request |
| 56 |
import base64 |
|
| 47 | 57 |
|
| 48 | 58 |
MONIT_XML_URL = ("http://{host}:{port}/_status?format=xml"
|
| 49 | 59 |
.format(host=os.getenv("host", "localhost"),
|
| ... | ... | |
| 57 | 67 |
|
| 58 | 68 |
def get_monit_status_xml(): |
| 59 | 69 |
try: |
| 60 |
conn = urllib.request.urlopen(MONIT_XML_URL) |
|
| 70 |
req = urllib.request.Request(url=MONIT_XML_URL) |
|
| 71 |
if os.getenv("password"):
|
|
| 72 |
auth_str = "%s:%s" % (os.getenv("username"), os.getenv("password"))
|
|
| 73 |
auth_bytes = bytes(auth_str, "utf-8") |
|
| 74 |
auth_base64_bytes = base64.b64encode(auth_bytes) |
|
| 75 |
auth_base64_str = str(auth_base64_bytes, "ascii") |
|
| 76 |
auth_value = "Basic %s" % auth_base64_str |
|
| 77 |
req.add_header("Authorization", auth_value)
|
|
| 78 |
conn = urllib.request.urlopen(req) |
|
| 61 | 79 |
except urllib.error.URLError as exc: |
| 62 | 80 |
conn = None |
| 63 | 81 |
if conn is None: |
Formats disponibles : Unified diff