2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
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.
31 # <domain> <age-of-newest-file> <size-of-newest-file> <total-size>
36 # Bandit complains about insecure protocol (B321). THis is the only possible protocol. Allow it.
37 from ftplib
import FTP
# nosec
43 # Fix pylint issues in case MySQLdb is not present
44 # pylint: disable=no-member
46 return MySQLdb
.connect(
50 passwd
=file('/etc/psa/.psa.shadow').read().strip(),
53 except MySQLdb
.Error
, e
:
54 sys
.stderr
.write("MySQL-Error %d: %s\n" % (e
.args
[0], e
.args
[1]))
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
72 return domain_collection
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
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():
96 output
.append('%s 4' % domain
) # Backup nicht konfiguriert
99 # Bandit complains about insecure protocol (B321). THis is the only possible protocol. Allow it.
101 p
['backup_ftp_settinghost'],
102 p
['backup_ftp_settinglogin'],
103 p
['backup_ftp_settingpassword'],
108 ftp
.retrlines('LIST %s' % p
['backup_ftp_settingdirectory'], callback
=files
.append
)
110 # -rw----r-- 1 b091045 cust 13660160 Dec 3 01:50 bla_v8_bla-v8.bla0.net_1212030250.tar
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]:
123 backups
.append(backup
)
126 output
.append('%s 5' % domain
) # Keine Sicherungen vorhanden
129 # Get total size of all files on FTP
132 def get_size(ftp_conn
, base_dir
, l
=None):
133 if l
and l
.split()[-1] in ['.', '..']:
137 if not l
or l
[0] == 'd':
138 subdir
= '/' + l
.split()[-1] if l
else ''
140 ftp_conn
.retrlines('LIST %s%s' % (base_dir
, subdir
), callback
=dir_files
.append
)
142 size
+= get_size(ftp_conn
, '%s%s' % (base_dir
, subdir
), ln
)
144 size
+= int(l
.split()[-5])
147 total_size
= get_size(ftp
, '')
150 '%s 0 %s %d %d' % (domain
, last_backup
[1].strftime('%s'), last_backup
[2], total_size
))
153 output
.append('%s 2 %s' % (domain
, e
))
155 # Write cache and output
156 sys
.stdout
.write('%s\n' % '\n'.join(output
))