-
-
Save kvasdopil/21fe47cea159992cf3cb94ab76bbacdf 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' | |
zabbix_server == 'server' | |
zabbix_port = 10051 | |
main() | |
def main(): | |
for serv in back_servs: | |
if server: | |
channel = connect(serv) | |
if channel: | |
metrics = do_work(channel) | |
disconnect(channel) | |
send_to_zabbix(metrics, zabbix_server, zabbix_port) | |
def disconnect(server): | |
print stdout.read() | |
stdout.close() | |
stdin.close() | |
client.close() | |
def connect(server): | |
# подключаемся по ssh и выполняем: | |
ssh = paramiko.SSHClient() | |
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
try: | |
ssh.connect(hostname=server, | |
port=port, | |
username=username, | |
key_filename=ssh_key, | |
timeout=3) | |
channel = ssh.invoke_shell() | |
return channel | |
except: | |
print '(!) invalid key or unavaliable ---> ' + server | |
ssh.close() | |
return False | |
def do_work(channel): | |
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''' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment