Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removes MySQL monitoring from Holland plugin #84

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 20 additions & 122 deletions holland_mysqldump.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# Description:
# Monitors backup configs which use the mysqldump Holland plugin.
#
# Monitors MySQL to ensure the credentials specified for Holland will succeed.
# Also monitors the dump file taken by Holland and the Holland logs.
#
# Requirements:
Expand All @@ -28,35 +27,23 @@
# Ensure file is executable (755)
#
# Example Criteria
# if (metric['sql_ping_succeeds'] == 'false') {
# return new AlarmStatus(CRITICAL, 'holland-plugin: MySQL is not running.');
# }
# if (metric['sql_creds_exist'] == 'false') {
# return new AlarmStatus(CRITICAL, 'holland-plugin: MySQL credentials file \
# does not exist.');
# }
# if (metric['sql_status_succeeds'] == 'false') {
# return new AlarmStatus(CRITICAL, 'holland-plugin: MySQL credentials do \
# not authenticate.');
# }
# if (metric['dump_age'] > 172800) {
# if (metric['dump_age'] > 172800) {
# return new AlarmStatus(CRITICAL, 'holland-plugin: mysqldump file is older \
# than 2d.');
# }
# if (metric['error_count'] > 0) {
# return new AlarmStatus(CRITICAL, 'holland-plugin: #{last_error}.');
# }
# than 2d.');
# }
# if (metric['error_count'] > 0) {
# return new AlarmStatus(CRITICAL, 'holland-plugin: #{last_error}.');
# }
#
# return new AlarmStatus(OK, 'holland-plugin: MySQL and Holland OK');
# return new AlarmStatus(OK, 'holland-plugin: Holland OK');

import os
import sys
import re
import time
import subprocess


def get_conf_value(config_file, key):
value = None
try:
config = open(config_file, 'r')
for line in config.readlines():
Expand Down Expand Up @@ -99,8 +86,6 @@ def get_log_position(self):
print "status error unable to retrieve log file modified time"
sys.exit(1)



def get_time_since_dump(self):
dump = os.path.join(self.directory, self.backupset, 'newest')
try:
Expand All @@ -118,87 +103,8 @@ class MySQL:
def __init__(self, backupset):
self.config_file = "/etc/holland/providers/mysqldump.conf"
self.backupset_config = "/etc/holland/backupsets/%s.conf" % backupset
if get_conf_value(self.backupset_config, 'user'):
self.user = get_conf_value(self.backupset_config, 'user')
self.password = get_conf_value(self.backupset_config, 'password')
else:
self.user = get_conf_value(self.config_file, 'user')
self.password = get_conf_value(self.config_file, 'password')

self.creds_files = get_conf_value(self.backupset_config,
'defaults-extra-file')
if not self.creds_files:
self.creds_files = get_conf_value(self.config_file,
'defaults-extra-file')

# return true if credentials set
def check_creds(self):
if (self.user and self.password):
return 'true'
elif self.creds_files:
for f in self.creds_files.split(','):
if os.access(f, os.F_OK):
return 'true'
return 'false'
else:
return 'false'

# return true if ping succeeds
def check_ping(self):
try:
DEVNULL = open(os.devnull, 'wb')
ping = subprocess.call(["/usr/bin/mysqladmin","ping"],
stdout=DEVNULL, stderr=DEVNULL)
except:
return 'false'
else:
DEVNULL.close()

if ping == 0:
return 'true'
else:
return 'false'

# return true if status succeeds
def check_status(self):
try:
DEVNULL = open(os.devnull, 'wb')
if self.creds_files:
for f in self.creds_files.split(','):
try:
status = subprocess.call([
"/usr/bin/mysqladmin",
"--defaults-file="+f,"status"],
stdout=DEVNULL,
stderr=DEVNULL)
if status == 0:
break
except:
status = 0
elif self.user and self.password:
status = subprocess.call([
"/usr/bin/mysqladmin",
"-u",self.user,
"-p"+self.password,
"status"],
stdout=DEVNULL,
stderr=DEVNULL)
else:
status = subprocess.call([
"/usr/bin/mysqladmin",
"status"],
stdout=DEVNULL,
stderr=DEVNULL)
except:
return 'false'
else:
DEVNULL.close()

if status == 0:
return 'true'
else:
return 'false'

if __name__ == '__main__':
if len(sys.argv) > 1:
backupset = sys.argv[1]
Expand All @@ -208,14 +114,12 @@ def check_status(self):
name = 'zzz_holland_backup_'+backupset

holland = Holland(backupset)

log_file = holland.get_log_file()
log_modified = holland.get_log_mod_time()
log_age = int(time.time() - log_modified)
dump_age = holland.get_time_since_dump()
log_pos = holland.get_log_position()


match = '\[ERROR\]'
split = '[ERROR]'
exclude = 'Warning: Skipping the data of table mysql.event'
Expand All @@ -231,9 +135,9 @@ def check_status(self):

# check file is accessible
if not os.access(log_file, os.R_OK):
print "status error unable to access file",log_file
print "status error unable to access file", log_file
sys.exit(1)

# read info from tracking file
if os.access(tracking_file, os.F_OK):
try:
Expand Down Expand Up @@ -262,16 +166,15 @@ def check_status(self):
else:
read_from_pos = 0
prev_date = 0



# find lines that match provided regex
matched_lines = []
reasons = []
try:
log = open(log_file, 'r')
log.seek(read_from_pos)
for line in log.readlines():
if re.search(match,line) and not re.search(exclude,line):
if re.search(match, line) and not re.search(exclude, line):
matched_lines.append(line)
reasons.append(line.split(split)[1].strip())
except:
Expand All @@ -280,7 +183,6 @@ def check_status(self):
else:
log.close()


# Get first and last error messages
if len(reasons) > 0:
first_error = reasons[0]
Expand All @@ -289,29 +191,25 @@ def check_status(self):
first_error = "none"
last_error = "none"

# write new tracking file - wait until after log has been read in case
# write new tracking file - wait until after log has been read in case
# of other issues
if log_modified > prev_date:
try:
tracking = open(tracking_file, 'w')
tracking.write(str(log_modified)+','+str(read_from_pos)+','
+str(log_pos))
tracking.write(str(log_modified)+','+str(read_from_pos) + ',' +
str(log_pos))
except:
print "status error unable to write to tracking file"
sys.exit(1)
else:
tracking.close()


# Finally check SQL
sql = MySQL(backupset)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the whole MySQL class could be removed.


print "status success holland checked"
print "metric log_age int64",log_age
print "metric dump_age int64",dump_age
print "metric error_count int64",len(matched_lines)
print "metric first_error string",first_error
print "metric last_error string",last_error
print "metric sql_creds_exist string",sql.check_creds()
print "metric sql_ping_succeeds string",sql.check_ping()
print "metric sql_status_succeeds string",sql.check_status()
print "metric log_age int64", log_age
print "metric dump_age int64", dump_age
print "metric error_count int64", len(matched_lines)
print "metric first_error string", first_error
print "metric last_error string", last_error