def send_mail(subject, content): msg = MIMEText(content, conf.content_type, conf.charset) msg['From'] = formataddr([conf.sender_name, conf.sender_addr]) msg['To'] = formataddr([conf.receiver_name, conf.receiver_addr]) msg['Subject'] = subject try: server = smtplib.SMTP_SSL(conf.host, conf.host_port) server.login(conf.sender_addr, conf.passwd) server.sendmail(conf.sender_addr, [ conf.receiver_addr, ], msg.as_string()) server.quit() logger.notice("Send mail success.") except Exception as e: logger.error("Send mail fail." + str(e))
def text_reply(msg): fromUser = itchat.search_friends(userName=msg["FromUserName"]) if fromUser is None: fromUser = {} toChatroom = itchat.search_chatrooms(userName=msg["ToUserName"]) if toChatroom is None: toChatroom = {} log.notice("{} -> {} : {}".format( fromUser.get("NickName", u"Stranger").encode("utf-8"), toChatroom.get("NickName", u"Stranger").encode("utf-8"), msg["Content"].encode("utf-8"))) if msg.isAt: text = msg.text.replace(username, u"").split(u"\u2005", 2)[-1].encode("utf-8") handle_user_msg(text, msg) elif fromUser is not None and fromUser.get("NickName", "") == username: text = msg.text.encode("utf-8") handle_user_msg(text, msg)
def print_content(msg): text = msg['Text'].encode("utf-8") user = msg['User']['UserName'] fromUser = itchat.search_friends(userName=msg["FromUserName"]) if fromUser is None: fromUserName = "******" else: fromUserName = fromUser["NickName"].encode("utf8") toUser = itchat.search_friends(userName=msg["ToUserName"]) if toUser is None: toUserName = "******" else: toUserName = toUser["NickName"].encode("utf8") if toUser is None: toUser = {} log.notice("{} -> {} : {}".format(fromUserName, toUserName, msg["Content"].encode("utf8"))) handle_user_msg(text, msg)
def insert_data(): try: db = MySQLdb.connect(db_conf.db_host, db_conf.db_user, \ db_conf.db_password, db_conf.db_database_name, charset='utf8') except Exception as e: logger.error("Connect to database fail.") return logger.notice("Connect to database success.") cursor = db.cursor() insert_sql = 'INSERT INTO {} ({}) VALUES({})' field_list = 'date, time, disk_total, disk_used, disk_used_rate, cpu_load_1_min, cpu_load_5_min, cpu_load_15_min, thread_count, memory_total, memory_used, memory_used_rate' data = collect_info() values = "'" + data["date"] + "', " + \ "'" + data['time'] + "', " + \ str(data["disk_total"]) + ', ' + \ str(data["disk_used"]) + ', ' + \ str(data["disk_used_rate"]) + ', ' + \ str(data["cpu_load_1_min"]) + ', ' + \ str(data["cpu_load_5_min"]) + ', ' + \ str(data["cpu_load_15_min"]) + ', ' + \ str(data["thread_count"]) + ', ' + \ str(data["memory_total"]) + ', ' + \ str(data["memory_used"]) + ', ' + \ str(data["memory_used_rate"]) sql = insert_sql.format(db_conf.db_table_name, field_list, values) try: cursor.execute(sql) except Exception as e: logger.error("Insert data to database fail.") return db.commit() search_sql = "SELECT * FROM {} WHERE date='{}' AND time='{}'" cursor.execute( search_sql.format(db_conf.db_table_name, data["date"], data["time"])) results = cursor.fetchall() if len(results) != 0: logger.notice("Insert data to database success.") else: logger.error("Insert data fail, no search data in database.")
def search_data(): try: db = MySQLdb.connect(db_conf.db_host, db_conf.db_user, db_conf.db_password, db_conf.db_database_name) except Exception as e: logger.error("Connect to database fail." + str(e)) return logger.notice("Connect to database success.") cursor = db.cursor() search_sql = "SELECT {} FROM {} WHERE date='{}'" yesterday = (date.today() + timedelta(days = -1)).strftime("%Y-%m-%d") try: cursor.execute(search_sql.format('*', db_conf.db_table_name, yesterday)) except Exception as e: logger.error("Execute sql instruction fail." + str(e)) return results = cursor.fetchall() db.close() if len(results) != 0: logger.notice("Search data success. Total : " + str(len(results))) return results else: logger.notice("Search data fail, no data in datebase by search key date = '" + yesterday + "'") return