def check(self): sql = SQLITE() data = sql.get_cron_all_data() print(data) now = datetime.datetime.now() print(now.day) print(now.year) print(now.minute) for i in data: datetime_object = datetime.datetime.strptime( str(now.year) + ":" + str(now.month) + ":" + str(now.day) + " " + i[3], "%Y:%m:%d %H:%M") print(datetime_object.strftime("%Y:%m:%d %H:%M")) print(now.strftime("%Y:%m:%d %H:%M")) if i[1] == '-1': day = calendar.monthrange(now.year, now.month)[1] else: day = i[1] if day == str(now.day): print((now - datetime_object).total_seconds()) if 0 < (now - datetime_object).total_seconds() < 605: print('run') task(int(i[2]))
def add_new_email(task_id): fields = { 'email': request.form['email'], 'comment': request.form['comment'], } sql = SQLITE() sql.add_new_mail_to(fields['email'], fields['comment'], task_id) return mail_table(task_id)
def add_hosts(task_id, group_id, id): sql = SQLITE() zab = get_zabbix() hosts = zab.get_host(sql.get_group_zabbix_id(group_id)) tmp = [] for object in hosts: tmp.append(object['host']) return render_template("hosts_create.html", data_table=tmp)
def hosts_table(task_id, group_id, id): print(id) sql = SQLITE() data = sql.get_hosts(group_id) return render_template('hosts_table.html', data_table=data, task=task_id, group=group_id, name=id)
def task_item_create(task_id, group_id, host_id, id, id_h): fields = { 'item': request.form['item'], 'comment': request.form['comment'], 'new_name': request.form['new_name'], } sql = SQLITE() sql.add_new_item_data(fields['item'], fields['comment'], fields['new_name'], task_id, host_id) return task_items_table(task_id, group_id, host_id, id, id_h)
def task_items_table(task_id, group_id, host_id, group, id): sql = SQLITE() data = sql.get_items_from_id(host_id) return render_template('item_table.html', task=task_id, group_id=group_id, data_table=data, name=id, host=host_id, group=group)
def item_create(task_id, group_id, host_id, id, id_h): zab = get_zabbix() sql = SQLITE() rez = sql.get_host_zabbix_id(host_id) items = zab.get_item(rez) return render_template("task_create.html", data_table=items, task=task_id, group=group_id, host=host_id)
def task_create_post(): fields = { 'task': request.form['task'], 'title': request.form['title'], 'begin': request.form['begin'], 'end': request.form['end'] } sql = SQLITE() sql.add_new_task(fields['task'], fields['title'], fields['begin'], fields['end']) return index()
def cro_add_post(task_id): fields = { 'type': request.form['type'], 'time': request.form['time'], 'day': request.form['day'] } if (fields['type'] == '0'): day = fields['day'] else: day = -1 sql = SQLITE() sql.add_new_cron_data(day, task_id, fields['time']) return cron(task_id)
def add_new_group(item_id): fields = { 'group': request.form['group'], 'comment': request.form['comment'], } zab = get_zabbix() data = zab.get_groups() zab_id = None for i in data: if i['name'] == fields['group']: zab_id = i['groupid'] sql = SQLITE() sql.add_new_group(fields['group'], fields['comment'], item_id, zab_id) return groups_table(item_id)
def task_host_create(task_id, group_id, id): fields = { 'host': request.form['host'], 'comment': request.form['comment'], } sql = SQLITE() zab = get_zabbix() hosts = zab.get_host(sql.get_group_zabbix_id(group_id)) zab_id = None for i in hosts: if i['host'] == fields['host']: zab_id = i['hostid'] sql.add_new_item_host(fields['host'], fields['comment'], task_id, group_id, zab_id) return hosts_table(task_id, group_id, id)
def delete_cron(id, task_id): sql = SQLITE() sql.delete_cron(id) return cron(task_id)
def cron(task_id): sql = SQLITE() data = sql.get_cron_data(task_id) return render_template('cron.html', data_table=data, task=task_id)
def groups_table(item_id): sql = SQLITE() data = sql.get_groups(item_id) return render_template('groups_table.html', data_table=data, data=item_id)
def delete_task(item_id): print(item_id) sql = SQLITE() sql.delete_task(item_id) return index()
def index(): sql = SQLITE() data = sql.get_tasks() return render_template('main_page.html', data_table=data)
def delete_host(task_id, group_id, id, host_id): print(id) sql = SQLITE() sql.delete_host(host_id) return hosts_table(task_id, group_id, id)
def mail_table(task_id): sql = SQLITE() data = sql.get_mail_to(task_id) return render_template('mail_to.html', data_table=data, task=task_id)
def delete_mails(item_id, task_id): sql = SQLITE() sql.delete_email(item_id) return mail_table(task_id)
def delete_group(task_id, item_id): sql = SQLITE() sql.delete_group(item_id) return groups_table(task_id)
def task(id): sql = SQLITE() Debug = True if True: html = """ <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>Отчет Zabbix</title> </head> <body> <table border="1"> <caption>Отчет</caption> <tr> <th></th> <th>Item</th> <th>Значение</th> <th>Комментарий</th> </tr>""" zabbix = ZABBIX() zabbix.auth("login", 'password') task_list = sql.get_all_data_to_manager(id) last_group = "" last_host = "" print(task_list) list = [] for data in task_list: list.append(ast.literal_eval(data[0])['itemid']) data_z = zabbix.get_items(list) result = {} for i in data_z: temp = i['itemid'] result[str(temp)] = i check = lambda T: T[2] if T[2] != "" else T[0] for data in task_list: if last_group != data[5]: last_group = data[5] html += "<tr> <th>" + data[5] + "</th></tr>" #continue if last_host != data[3]: last_host = data[3] html += "<tr> <th></th><th></th><th></th><th></th></tr>" html += "<tr> <th></th><th>" + data[ 3] + "</th><th></th><th></th></tr>" #continue #if html += "<tr> <th></th><th>" + check(data) + "</th><th>" \ + result[ast.literal_eval(data[0])['itemid']]['lastvalue']+"</th><th>" +\ data[1] + "</th></tr>" html += """</table> </body> </html>""" f = open('text.html', 'w') f.write(html) f.close() task = sql.get_task_data(id) mails = sql.get_mail_to_manager(id) mail = " ".join(mails) cmd = 'echo "' + task[0][3] + '\n\n\n' + task[0][4] + '" | mailx -v -r "' \ + task[0][2] +'" -s "Tor notification" ' \ '-S smtp="192.168.10.10:25" -a "./text.html" ' + mail # cmd = 'cat text.html | mailx -v -r "*****@*****.**" ' \ # '-s "$(echo "This is the subject\nContent-Type: text/html")" ' \ # '-S smtp="192.168.10.10:25" -a "./text.html" ' + mail print(cmd) os.system(cmd) #
def delete_item(item_id, task_id, group_id, group, host_id, id): print(item_id) sql = SQLITE() sql.delete_item_data(item_id) return task_items_table(task_id, group_id, host_id, group, id)