Cleanup config.nodes_of
[check_mk.git] / agents / plugins / plesk_backups
blob0f7bbe320015e8d7968b19411f90a979f43d6a06
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
11 # +------------------------------------------------------------------+
13 # This file is part of Check_MK.
14 # The official homepage is at http://mathias-kettner.de/check_mk.
16 # check_mk is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation in version 2. check_mk is distributed
19 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
20 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
21 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
22 # tails. You should have received a copy of the GNU General Public
23 # License along with GNU Make; see the file COPYING. If not, write
24 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 # Boston, MA 02110-1301 USA.
27 # Monitors FTP backup spaces of plesk domains.
28 # Data format
30 # <<<plesk_backups>>>
31 # <domain> <age-of-newest-file> <size-of-newest-file> <total-size>
33 import datetime
34 import sys
35 import time
36 # Bandit complains about insecure protocol (B321). THis is the only possible protocol. Allow it.
37 from ftplib import FTP # nosec
39 import MySQLdb
42 def connect():
43 # Fix pylint issues in case MySQLdb is not present
44 # pylint: disable=no-member
45 try:
46 return MySQLdb.connect(
47 host='localhost',
48 db='psa',
49 user='admin',
50 passwd=file('/etc/psa/.psa.shadow').read().strip(),
51 charset='utf8',
53 except MySQLdb.Error, e:
54 sys.stderr.write("MySQL-Error %d: %s\n" % (e.args[0], e.args[1]))
55 sys.exit(1)
58 def get_domains():
59 cursor = db.cursor()
60 cursor2 = db.cursor()
62 cursor.execute('SELECT id, name FROM domains')
63 domain_collection = {}
64 for this_domain_id, this_domain in cursor.fetchall():
65 cursor2.execute('SELECT param, value FROM BackupsSettings '
66 'WHERE id = %d AND type = \'domain\'' % this_domain_id)
67 params = dict(cursor2.fetchall())
68 domain_collection[this_domain] = params
70 cursor2.close()
71 cursor.close()
72 return domain_collection
76 # MAIN
79 db = connect()
81 # 1. Virtual Hosts / Domains auflisten
82 # 2. Backupkonfiguration herausfinden
83 domains = get_domains()
85 # 3. Per FTP verbinden
86 # 4. Alter und Größe der neuesten Datei herausfinden
87 # 5. Größe aller Dateien in Summe herausfinden
89 # 6. Neuer Monat?
90 # 7. Auf FTP neues Verzeichnis anlegen: <kunde>_2012<monat>
91 # 8. Konfiguration in Plesk anpassen
92 output = ['<<<plesk_backups>>>']
93 for domain, p in domains.iteritems():
94 try:
95 if not p:
96 output.append('%s 4' % domain) # Backup nicht konfiguriert
97 continue
99 # Bandit complains about insecure protocol (B321). THis is the only possible protocol. Allow it.
100 ftp = FTP( # nosec
101 p['backup_ftp_settinghost'],
102 p['backup_ftp_settinglogin'],
103 p['backup_ftp_settingpassword'],
106 # Zeilen holen
107 files = []
108 ftp.retrlines('LIST %s' % p['backup_ftp_settingdirectory'], callback=files.append)
109 # example line:
110 # -rw----r-- 1 b091045 cust 13660160 Dec 3 01:50 bla_v8_bla-v8.bla0.net_1212030250.tar
112 # Zeilen formatieren
113 last_backup = None
114 backups = []
115 for line in files:
116 parts = line.split()
117 if parts[-1].endswith('.tar'):
118 dt = datetime.datetime(*time.strptime(parts[-1][-14:-4], '%y%m%d%H%M')[0:5])
119 backup = (parts[-1], dt, int(parts[-5]))
121 if not last_backup or dt > last_backup[1]:
122 last_backup = backup
123 backups.append(backup)
125 if not backups:
126 output.append('%s 5' % domain) # Keine Sicherungen vorhanden
127 continue
129 # Get total size of all files on FTP
130 f = []
132 def get_size(ftp_conn, base_dir, l=None):
133 if l and l.split()[-1] in ['.', '..']:
134 return 0
136 size = 0
137 if not l or l[0] == 'd':
138 subdir = '/' + l.split()[-1] if l else ''
139 dir_files = []
140 ftp_conn.retrlines('LIST %s%s' % (base_dir, subdir), callback=dir_files.append)
141 for ln in dir_files:
142 size += get_size(ftp_conn, '%s%s' % (base_dir, subdir), ln)
143 else:
144 size += int(l.split()[-5])
145 return size
147 total_size = get_size(ftp, '')
149 output.append(
150 '%s 0 %s %d %d' % (domain, last_backup[1].strftime('%s'), last_backup[2], total_size))
152 except Exception, e:
153 output.append('%s 2 %s' % (domain, e))
155 # Write cache and output
156 sys.stdout.write('%s\n' % '\n'.join(output))