Created
January 13, 2021 11:56
-
-
Save mohatb/2e1019a238f65636e5fbcbc85cb4e4d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| apiVersion: apps/v1 | |
| kind: DaemonSet | |
| metadata: | |
| labels: | |
| component: alert-on-reboot-la | |
| name: alert-on-reboot-la | |
| namespace: kube-system | |
| spec: | |
| selector: | |
| matchLabels: | |
| component: alert-on-reboot-la | |
| tier: node | |
| template: | |
| metadata: | |
| labels: | |
| component: alert-on-reboot-la | |
| tier: node | |
| spec: | |
| containers: | |
| - name: alert-on-reboot-la | |
| image: alpine | |
| imagePullPolicy: IfNotPresent | |
| command: | |
| - nsenter | |
| - --target | |
| - "1" | |
| - --mount | |
| - --uts | |
| - --ipc | |
| - --net | |
| - --pid | |
| - -- | |
| - sh | |
| - -c | |
| - | | |
| cat <<'EOF' >/etc/systemd/system/noderebootmonitor.service | |
| [Unit] | |
| Description=Started monitoring reboot | |
| Requires=network.target | |
| DefaultDependencies=no | |
| Before=shutdown.target reboot.target | |
| [Service] | |
| Type=oneshot | |
| RemainAfterExit=true | |
| ExecStart=/usr/bin/python3 /tmp/pyup.py | |
| ExecStop=/usr/bin/python3 /tmp/pyreboot.py | |
| [Install] | |
| WantedBy=multi-user.target | |
| EOF | |
| cat <<'EOF' > /tmp/pyup.py | |
| import json | |
| import requests | |
| import datetime | |
| import hashlib | |
| import hmac | |
| import base64 | |
| import subprocess | |
| from subprocess import Popen | |
| from subprocess import PIPE | |
| # Update the customer ID to your Log Analytics workspace ID | |
| customer_id = 'xxx-xxx...' | |
| # For the shared key, use either the primary or the secondary Connected Sources client authentication key | |
| shared_key = "xxxxxxxxx" | |
| # The log type is the name of the event that is being submitted | |
| log_type = 'AKSNodeReboot' | |
| # An example JSON web monitor object | |
| commands = ''' | |
| NodeStatus=`echo node started` | |
| UTCScriptRunTime=`date "+%F %T"` | |
| #hostname | |
| hostname=`hostname` 2> /dev/null | |
| # Get Linux Distribution | |
| distro=`lsb_release -d | awk '{print $2 $3}'` | |
| # Get Server uptime | |
| if [ -f "/proc/uptime" ]; then | |
| uptime=`cat /proc/uptime` | |
| uptime=${uptime%%.*} | |
| seconds=$(( uptime%60 )) | |
| minutes=$(( uptime/60%60 )) | |
| hours=$(( uptime/60/60%24 )) | |
| days=$(( uptime/60/60/24 )) | |
| uptime="$days days, $hours hours, $minutes minutes, $seconds seconds" | |
| else | |
| uptime="" | |
| fi | |
| # Get VM private IP Address | |
| IPAddress=`ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'` 2> /dev/null | |
| # Get VM Public IP Address | |
| PublicIP=`wget http://ipecho.net/plain -O - -q ; echo` 2> /dev/null | |
| printf '{"UTCScriptRunTime":"%s", "NodeStatus":"%s", "hostname":"%s","distro":"%s","uptime":"%s","IPAddress":"%s","PublicIP":"%s"}\n' "$UTCScriptRunTime" "$NodeStatus" "$hostname" "$distro" "$uptime" "$IPAddress" "$PublicIP" | |
| ''' | |
| process = subprocess.Popen('/bin/bash', stdin=subprocess.PIPE, stdout=subprocess.PIPE) | |
| body, err = process.communicate(commands.encode('utf-8')) | |
| ##################### | |
| ######Functions###### | |
| ##################### | |
| # Build the API signature | |
| def build_signature(customer_id, shared_key, date, content_length, method, content_type, resource): | |
| x_headers = 'x-ms-date:' + date | |
| string_to_hash = method + "\n" + str(content_length) + "\n" + content_type + "\n" + x_headers + "\n" + resource | |
| bytes_to_hash = bytes(string_to_hash, encoding="utf-8") | |
| decoded_key = base64.b64decode(shared_key) | |
| encoded_hash = base64.b64encode(hmac.new(decoded_key, bytes_to_hash, digestmod=hashlib.sha256).digest()).decode() | |
| authorization = "SharedKey {}:{}".format(customer_id,encoded_hash) | |
| return authorization | |
| # Build and send a request to the POST API | |
| def post_data(customer_id, shared_key, body, log_type): | |
| method = 'POST' | |
| content_type = 'application/json' | |
| resource = '/api/logs' | |
| rfc1123date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT') | |
| content_length = len(body) | |
| signature = build_signature(customer_id, shared_key, rfc1123date, content_length, method, content_type, resource) | |
| uri = 'https://' + customer_id + '.ods.opinsights.azure.com' + resource + '?api-version=2016-04-01' | |
| headers = { | |
| 'content-type': content_type, | |
| 'Authorization': signature, | |
| 'Log-Type': log_type, | |
| 'x-ms-date': rfc1123date | |
| } | |
| response = requests.post(uri,data=body, headers=headers) | |
| if (response.status_code >= 200 and response.status_code <= 299): | |
| print('Accepted') | |
| else: | |
| print("Response code: {}".format(response.status_code)) | |
| post_data(customer_id, shared_key, body, log_type) | |
| EOF | |
| cat <<'EOF' > /tmp/pyreboot.py | |
| import json | |
| import requests | |
| import datetime | |
| import hashlib | |
| import hmac | |
| import base64 | |
| import subprocess | |
| from subprocess import Popen | |
| from subprocess import PIPE | |
| # Update the customer ID to your Log Analytics workspace ID | |
| customer_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' | |
| # For the shared key, use either the primary or the secondary Connected Sources client authentication key | |
| shared_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | |
| # The log type is the name of the event that is being submitted | |
| log_type = 'AKSNodeReboot' | |
| # An example JSON web monitor object | |
| commands = ''' | |
| NodeStatus=`echo node rebooted` | |
| UTCScriptRunTime=`date "+%F %T"` | |
| #hostname | |
| hostname=`hostname` 2> /dev/null | |
| # Get Linux Distribution | |
| distro=`lsb_release -d | awk '{print $2 $3}'` | |
| # Get Server uptime | |
| if [ -f "/proc/uptime" ]; then | |
| uptime=`cat /proc/uptime` | |
| uptime=${uptime%%.*} | |
| seconds=$(( uptime%60 )) | |
| minutes=$(( uptime/60%60 )) | |
| hours=$(( uptime/60/60%24 )) | |
| days=$(( uptime/60/60/24 )) | |
| uptime="$days days, $hours hours, $minutes minutes, $seconds seconds" | |
| else | |
| uptime="" | |
| fi | |
| # Get VM private IP Address | |
| IPAddress=`ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'` 2> /dev/null | |
| # Get VM Public IP Address | |
| PublicIP=`wget http://ipecho.net/plain -O - -q ; echo` 2> /dev/null | |
| printf '{"UTCScriptRunTime":"%s", "NodeStatus":"%s", "hostname":"%s","distro":"%s","uptime":"%s","IPAddress":"%s","PublicIP":"%s"}\n' "$UTCScriptRunTime" "$NodeStatus" "$hostname" "$distro" "$uptime" "$IPAddress" "$PublicIP" | |
| ''' | |
| process = subprocess.Popen('/bin/bash', stdin=subprocess.PIPE, stdout=subprocess.PIPE) | |
| body, err = process.communicate(commands.encode('utf-8')) | |
| ##################### | |
| ######Functions###### | |
| ##################### | |
| # Build the API signature | |
| def build_signature(customer_id, shared_key, date, content_length, method, content_type, resource): | |
| x_headers = 'x-ms-date:' + date | |
| string_to_hash = method + "\n" + str(content_length) + "\n" + content_type + "\n" + x_headers + "\n" + resource | |
| bytes_to_hash = bytes(string_to_hash, encoding="utf-8") | |
| decoded_key = base64.b64decode(shared_key) | |
| encoded_hash = base64.b64encode(hmac.new(decoded_key, bytes_to_hash, digestmod=hashlib.sha256).digest()).decode() | |
| authorization = "SharedKey {}:{}".format(customer_id,encoded_hash) | |
| return authorization | |
| # Build and send a request to the POST API | |
| def post_data(customer_id, shared_key, body, log_type): | |
| method = 'POST' | |
| content_type = 'application/json' | |
| resource = '/api/logs' | |
| rfc1123date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT') | |
| content_length = len(body) | |
| signature = build_signature(customer_id, shared_key, rfc1123date, content_length, method, content_type, resource) | |
| uri = 'https://' + customer_id + '.ods.opinsights.azure.com' + resource + '?api-version=2016-04-01' | |
| headers = { | |
| 'content-type': content_type, | |
| 'Authorization': signature, | |
| 'Log-Type': log_type, | |
| 'x-ms-date': rfc1123date | |
| } | |
| response = requests.post(uri,data=body, headers=headers) | |
| if (response.status_code >= 200 and response.status_code <= 299): | |
| print('Accepted') | |
| else: | |
| print("Response code: {}".format(response.status_code)) | |
| post_data(customer_id, shared_key, body, log_type) | |
| EOF | |
| chmod +x /tmp/pyup.py | |
| chmod +x /tmp/pyreboot.py | |
| chmod u+x /etc/systemd/system/noderebootmonitor.service | |
| systemctl start noderebootmonitor | |
| sudo systemctl enable noderebootmonitor | |
| while true; do sleep 100000; done | |
| resources: | |
| requests: | |
| cpu: 10m | |
| securityContext: | |
| privileged: true | |
| dnsPolicy: ClusterFirst | |
| hostPID: true | |
| tolerations: | |
| - effect: NoSchedule | |
| operator: Exists | |
| restartPolicy: Always | |
| updateStrategy: | |
| type: OnDelete |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment