-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcheckMongoDBVersion.py
executable file
·68 lines (53 loc) · 2.2 KB
/
checkMongoDBVersion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##
# Tart Monitoring
#
# Copyright (c) 2013, Tart İnternet Teknolojileri Ticaret AŞ
#
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby
# granted, provided that the above copyright notice and this permission notice appear in all copies.
#
# The software is provided "as is" and the author disclaims all warranties with regard to the software including all
# implied warranties of merchantability and fitness. In no event shall the author be liable for any special, direct,
# indirect, or consequential damages or any damages whatsoever resulting from loss of use, data or profits, whether
# in an action of contract, negligence or other tortious action, arising out of or in connection with the use or
# performance of this software.
##
import sys
import urllib2
import pymongo
import re
from HTMLParser import HTMLParser
class DownloadHTMLParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.__inHeader = False
self.__headers = []
def handle_starttag(self, tag, attrs):
if tag in ('h1', 'h2', 'h3', 'h4'):
self.__inHeader = True
def handle_data(self, data):
if self.__inHeader:
self.__headers.append(data)
def lastMinorVersion(self, majorVersion):
for header in self.__headers:
version = re.search(majorVersion + '\.[0-9]+', header)
if version:
return version.group()
if len(sys.argv) < 2:
raise Exception('No argument.')
if len(sys.argv) > 2:
raise Exception('Too many arguments.')
serverInfo = pymongo.Connection(sys.argv[1]).server_info()
majorVersion = str(serverInfo['versionArray'][0]) + '.' + str(serverInfo['versionArray'][1])
response = urllib2.urlopen('http://www.mongodb.org/downloads')
parser = DownloadHTMLParser()
parser.feed(response.read())
lastMinorVersion = parser.lastMinorVersion(majorVersion)
if not lastMinorVersion:
raise Exception('Could not get the last minor version.')
print('Current version ' + serverInfo['version'] + ' last minor version: ' + lastMinorVersion)
if serverInfo['version'] != lastMinorVersion:
sys.exit(1)
sys.exit(0)