Created
February 14, 2017 13:03
-
-
Save anonymous/1c325a23b186c62edd0087394105ebd6 to your computer and use it in GitHub Desktop.
This file contains 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
import os, time, re | |
import paramiko | |
from zbxsend import Metric, send_to_zabbix | |
#Параметры для подключения по ssh | |
username='root' # ssh: login | |
ssh_key='/root/.ssh/id_dsa.pub' # ssh: password | |
port=22 # ssh: port | |
back_servs = [ea1-wal-backups, ea1-wal-backup2, ea1-wal-backup3, ea1-wal-backup4] | |
zabbix_key = 'custom.pgbackup.status' | |
for serv in back_servs: | |
# подключаемся по ssh и выполняем: | |
ssh = paramiko.SSHClient() | |
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
if server: | |
try: | |
ssh.connect(hostname=server, | |
port=port, | |
username=username, | |
key_filename=ssh_key, | |
timeout=3) | |
except: | |
print '(!) invalid key or unavaliable ---> ' + server | |
ssh.close() | |
continue | |
else: | |
channel = ssh.invoke_shell() | |
stdin = channel.makefile('wb') | |
stdout = channel.makefile('rb') | |
stdin.write(''' | |
host = serv.backup#!!!!! | |
timestamp = int(time.time()) | |
zabbix_server = 'localhost' | |
back_dir = '/var/backup' | |
zabbix_metrics = [] | |
#При необходимости добавляем сюда базы, которые не нужно мониторить | |
db_to_remove = set(["oper-db-edo1", "osr-ed19-db1"]) | |
#Составляем список всех папок в директории /var/backup | |
folders_list_all = os.listdir(back_dir) | |
folders_list = filter(lambda x: x not in db_to_remove, folders_list_all) | |
#Здесь окажется список всех баз для мониторинга, за исключением папок для не баз | |
db_list = [] | |
for folder in folders_list: | |
if re.search(r'db', folder): | |
db_list.append(folder) | |
print db_list | |
# #Передвигаемся по подпапкам каждой директории с бэкапом, ища пустые папки и файлы, старше 12 дней | |
# for db in db_list: | |
# try: | |
# half_path = "/".join( [back_dir, db, "pitr"] ) | |
# subfolders = os.listdir(half_path) | |
# except OSError: | |
# #print db + ".empty_folder" | |
# zabbix_metrics.append( Metric( host, items_key, 'Папка с бэкапом базы ' + db + ' пуста', timestamp )) | |
# continue | |
# timeout = time.time() - 60*60*24*12 | |
# for subfolder in subfolders: | |
# full_path = "/".join( [half_path, subfolder, 'xlog'] ) | |
# xlog_list = os.listdir(full_path) | |
# try: | |
# xlog_min_time = os.path.getmtime("/".join( [full_path, xlog_list[0]] )) | |
# except IndexError: | |
# #print db + ".no_xlog" | |
# zabbix_metrics.append( Metric( host, items_key, 'Папка с бэкапом xlog базы ' + db + ' пуста', timestamp )) | |
# break | |
# for xlog in xlog_list: | |
# try: | |
# xlog_time = os.path.getmtime("/".join( [full_path, xlog] )) | |
# if xlog_time < timeout: | |
# #print db + ".old_backup" | |
# zabbix_metrics.append( Metric( host, items_key, 'Проверить дату выполнения бэкапа базы ' + db ' и наличие старых xlog' , timestamp )) | |
# break | |
# except OSError: | |
# pass''' | |
print stdout.read() | |
stdout.close() | |
stdin.close() | |
client.close() | |
#здесь заканчивается подключение по ssh | |
#Send info to zabbix | |
#send_to_zabbix(zabbix_metrics, zabbix_server, 10051) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment