-
Notifications
You must be signed in to change notification settings - Fork 0
/
zmb-domain-summary.py
284 lines (219 loc) · 9.12 KB
/
zmb-domain-summary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python
import ldap
import os
from mod_zmb_mylogformatter import logger as log
from collections import defaultdict
from timeit import default_timer as timer
__author__ = 'yodog'
__version__ = '2019.07.12.1610'
# ------------------------------------------------------------------------------
# Definicoes
# ------------------------------------------------------------------------------
# Iniciar medicao de tempo de execucao deste script
start_time_script = timer()
# Tamanho das informacoes exibidas na tela
TRUNCATE = 99
# Algumas informacoes para serem usadas adiante
FQDN = os.popen('hostname -f').read().split()[0]
SCRIPTNAME = os.path.splitext(os.path.basename(__file__))[0]
SCRIPTPATH = os.path.realpath(__file__)
# ------------------------------------------------------------------------------
# Funcoes
# ------------------------------------------------------------------------------
# * * *
# Returns ldap connection object
# * * *
def createLdapConn():
LDAP_SERVER = "1.2.3.4"
LDAP_USER = "cn=config"
LDAP_PASS = "password"
log.debug('Criando conexao ldap://%s', LDAP_SERVER)
connection = ldap.initialize("ldap://%s" % LDAP_SERVER)
connection.protocol_version = ldap.VERSION3
connection.bind (LDAP_USER, LDAP_PASS)
return connection
# * * *
# Returns dict with all zimbra accounts and the requested attrs
# * * *
def zmbGetAllAccounts():
log.info('')
log.info('Consultando contas de usuario')
start_time_ldap = timer()
LDAP_BASE_DN = "dc=br"
LDAP_FILTRO = """
(&
(objectClass=zimbraAccount)
(zimbraMailDeliveryAddress=*)
(!
(|
(objectClass=zimbraCalendarResource)
(zimbraExternalUserMailAddress=*)
)
)
)
"""
LDAP_ATRIBUTOS = [
"zimbraAccountStatus",
"zimbraIsAdminAccount",
"zimbraIsDelegatedAdminAccount",
"zimbraIsSystemAccount",
"zimbraMailDeliveryAddress",
]
# Remover identacao da string LDAP_FILTRO (causa erro no consulta ldap)
LDAP_FILTRO = "".join(LDAP_FILTRO.split())
# Exportar esta constante para o escopo global pois preciso usa-la no fim do script
if 'LDAP_ATRIBUTOS' not in globals(): globals()['LDAP_ATRIBUTOS'] = LDAP_ATRIBUTOS
connection = createLdapConn()
ldapsearch = connection.search_s(LDAP_BASE_DN, ldap.SCOPE_SUBTREE, LDAP_FILTRO, LDAP_ATRIBUTOS)
end_time_ldap = timer() - start_time_ldap
log.info('... %d conta(s) encontrado(s) em %s segundos', len(ldapsearch), round(end_time_ldap,2))
log.debug(str(ldapsearch))
log.info('')
return sorted(ldapsearch)
# * * *
# Pesquisar ldap e criar banco de dados indexado por dominio
# * * *
def createDomainDatabase():
log.info('-' * TRUNCATE)
log.info('Criando banco de dados indexado por dominio')
bd = defaultdict(list)
for uid, entry in zmbGetAllAccounts():
email = entry['zimbraMailDeliveryAddress'][0]
user, domain = email.split('@')
data = {k:v[0] for k,v in entry.items()}
bd[domain].append(data)
log.info('Banco de dados criado')
log.debug(str(bd))
return bd
# * * *
# Criar array/lista com os contadores para o sumario
# Depois e salvo em um arquivo txt formatado igual ao 'domain summary'
# gerado pelo '/opt/zimbra/bin/zmaccts'
# * * *
def createSummaryArray():
log.info('-' * TRUNCATE)
log.info('Criando banco de dados com contadores para o sumario')
summary = []
for domain, domaindata in bd.items():
s = dict(domain=domain, active=0, closed=0, locked=0, maintenance=0, total=0, admin=0, delegatedadmin=0, system=0, pending=0)
for userdata in domaindata:
if 'zimbraIsAdminAccount' in userdata:
if userdata['zimbraIsAdminAccount'].upper() == 'TRUE':
s['admin'] += 1
continue
if 'zimbraIsDelegatedAdminAccount' in userdata:
if userdata['zimbraIsDelegatedAdminAccount'].upper() == 'TRUE':
s['delegatedadmin'] += 1
continue
if 'zimbraIsSystemAccount' in userdata:
s['system'] += 1
continue
zimbraAccountStatus = userdata['zimbraAccountStatus']
s[zimbraAccountStatus] += 1
s['total'] += 1
s['*'] = '*'
summary.append(s)
log.info('')
log.info('Banco de dados criado')
log.debug(str(summary))
return summary
# * * *
# Pretty print a list of dictionaries (myDict) as a dynamically sized table.
# If column names (colList) aren't specified, they will show in random order.
# * * *
def printTable(myDict, colList=None, fileName=None):
if not colList: colList = list(myDict[0].keys() if myDict else [])
myList = []
for item in myDict: myList.append([str(item[col] if item[col] is not None else '') for col in colList])
myList = sorted(myList)
myList.insert(0, colList) # header
colSize = [max(map(len,col)) for col in zip(*myList)]
formatStr = ' | '.join(["{{:<{}}}".format(i) for i in colSize])
myList.insert(1, ['-' * i for i in colSize]) # Seperating line
if fileName:
with open(fileName, 'wb') as f:
for item in myList: f.write( formatStr.format(*item) + '\n' )
else:
for item in myList: print(formatStr.format(*item))
# * * *
# Send text file as email body
# * * *
def sendMail(textfile, send=False):
import smtplib
from email.mime.text import MIMEText
with open(textfile, 'rb') as fp:
msg = MIMEText(fp.read())
msg['Subject'] = 'Zimbra Domain Summary'
msg['From'] = SCRIPTNAME + '@' + FQDN
msg['To'] = 'lista@dominio.com.br'
# postar sem autenticacao com starttls
s = smtplib.SMTP('smtp.dominio.com.br', 587)
s.starttls()
if send:
s.sendmail(msg['From'], msg['To'], msg.as_string())
s.quit()
# postar com autenticacao na porta segura
#s = smtplib.SMTP_SSL('mobile.dominio.com.br', 465)
#s.login('testador@dominio.com.br', 'senha')
#if send:
# s.sendmail(msg['From'], msg['To'], msg.as_string())
#s.quit()
return str(msg)
# ------------------------------------------------------------------------------
# __main__
# ------------------------------------------------------------------------------
# Wrapping the execution in __main__ makes it safe to be imported
# If the module is the main program it will execute the functions
# If the module is imported by another, only load the functions
# ------------------------------------------------------------------------------
if __name__ == '__main__':
# --------------------------------------------------------------------------
# Argumentos
# --------------------------------------------------------------------------
import argparse
arg_parser = argparse.ArgumentParser(
description = 'Relatorio de aproriacao do Zimbra',
epilog = 'Arquivos de saida: /tmp/<domain>.csv',
formatter_class = lambda prog: argparse.RawTextHelpFormatter(prog, max_help_position=999)
)
arg_parser.add_argument('-v', '--version', action='version', version=__version__)
argslist = [
('-l', '--loglevel', '(default: %(default)s) Alterar verbosidade', dict(
type=str.upper, const='logging.', default='WARN', nargs='?')
),
('-s', '--sendmail', '(default: %(default)s) Enviar resultado por email', dict(action='store_true')),
('-t', '--testmode', '(default: %(default)s) Ativar modo teste (rapido)', dict(action='store_true')),
]
for argshort, arglong, desc, options in argslist:
arg_parser.add_argument(argshort, arglong, help=desc, **options)
args = arg_parser.parse_args()
# --------------------------------------------------------------------------
# Inicio
# --------------------------------------------------------------------------
# Ajustar nivel de verbosidade do log, caso o usuario tenha passado algum
log.setLevel(args.loglevel)
# Pesquisar ldap e criar banco de dados indexado por dominio
bd = createDomainDatabase()
# Array com contadores do sumario
summary = createSummaryArray()
# Arquivo em que o sumario sera gravado
summaryfile = '/tmp/domainsummary.txt'
log.info('-' * TRUNCATE)
log.info('Salvando arquivo de sumario em %s', summaryfile)
log.debug(str(summary))
# Gerar tabela com o conteudo do sumario
printTable(
summary,
['domain', 'active', 'closed', 'locked', 'maintenance', 'total', '*', 'admin', 'delegatedadmin', 'system', 'pending'],
summaryfile
)
# Imprimir resultado. Envia-lo por email?
log.info('Enviar sumario por email: %s', args.sendmail)
msg = sendMail(summaryfile, args.sendmail)
print ('\n'), msg
# --------------------------------------------------------------------------
# Fim
# --------------------------------------------------------------------------
end_time_script = timer() - start_time_script
log.info('-' * TRUNCATE)
log.info('Script finalizado em %s segundos', round(end_time_script,2))