Created
December 3, 2018 01:01
-
-
Save watahani/526a6cdb98dbc27a4c961cea4cbde2ba 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
#!/usr/bin/env python | |
''' | |
put this file to "/etc/mackerel-agent/check-gitlab-version.py" | |
and add /etc/mackerel-agent/mackerel-agent.conf | |
if env not set, hostname is got from hostname command | |
[plugin.checks.gitlab_version] | |
command = "python /etc/mackerel-agent/check-gitlab-version.py" | |
env = { HOST = "https://your.gitlab.host" } | |
max_check_attempts = 3 | |
check_interval = 240 | |
memo = "gitlab ce version check" | |
''' | |
import os | |
import sys | |
import requests | |
import base64 | |
import re | |
API_POINT = 'https://version.gitlab.com/check.svg?gitlab_info=' | |
EXPECTED_TEXT_LIST = ['update asap', 'update available', 'up-to-date'] | |
HOST = os.environ.get('HOST') or 'https://' + os.uname()[1] | |
def get_gitlab_version(): | |
with open('/opt/gitlab/version-manifest.txt', 'r') as f: | |
manifest = f.read() | |
gitlab_ce_version = r'gitlab-ce\s+([\d+\.*]+)' | |
result = re.search(gitlab_ce_version, manifest) | |
if result: | |
return result.group(1) | |
else: | |
return None | |
def fetch_update_status(version_json): | |
encoded_version = base64.b64encode( | |
version_json.encode('utf-8')).decode('ascii') | |
url = API_POINT + encoded_version | |
headers = {'Referer': HOST} | |
r = requests.get(url, headers=headers) | |
if r.status_code >= 300: | |
print(API_POINT + " return status: " + r.status_code) | |
print("api might be changed...") | |
print('check "/etc/mackerel-agent/check-gitlab-version.py" script !') | |
sys.exit(3) | |
result = r.text | |
regex = r'>(.+?)<\/text>' | |
result = re.search(regex, result) | |
if not result: | |
print("status not match") | |
print("api might be changed...") | |
print('check "/etc/mackerel-agent/check-gitlab-version.py" script !') | |
sys.exit(3) | |
return result.group(1) | |
if __name__ == '__main__': | |
version = get_gitlab_version() | |
if not version: | |
print('can\'t get gitlab-ce version from /opt/gitlab/version-manifest.txt') | |
print('check "/etc/mackerel-agent/check-gitlab-version.py" script !') | |
sys.exit(3) | |
print('gitlab-ce version check...') | |
print('') | |
print('gitlab-ce version: {}'.format(version)) | |
print('') | |
version_json = '{{"version":"{}"}}'.format(version) | |
result = fetch_update_status(version_json) | |
if result not in EXPECTED_TEXT_LIST: | |
print("unknown status: " + result) | |
print('check "/etc/mackerel-agent/check-gitlab-version.py" script !') | |
sys.exit(3) | |
if result == 'update asap': | |
print('gitlab-ce should update asap.') | |
print('ssh to gitlab host and update gitlab.') | |
print('see https://about.gitlab.com/update/#ubuntu') | |
sys.exit(2) | |
elif result == 'update available': | |
print('new version gitlab is available') | |
print('see https://about.gitlab.com/update/#ubuntu') | |
sys.exit(1) | |
else: | |
print('gitlab ce up-to-date') | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment