def func(_index): new_idct = defaultdict(lambda:defaultdict(dict)) new_idct = device_idct fuc_ip = new_idct[_index]['ip'] fuc_acl = new_idct[_index]['acl'] fuc_rule = new_idct[_index]['rule'] fuc_description = new_idct[_index]['description'] fuc_last = new_idct[_index]['last_counter'] fuc_muti_mail = new_idct[_index]['muti_mail'] fuc_muti_phone = new_idct[_index]['muti_phone'] fuc_fazhi = new_idct[_index]['fazhi'] print '---',_index,'/',linecount,'---',fuc_ip,fuc_acl,'---',fuc_description,fuc_rule,'---[',fuc_last,']---',fuc_muti_mail,fuc_muti_phone,fuc_fazhi,'---' #get new status newcounter = getAclCounter(fuc_ip,fuc_acl,fuc_rule) changenum = int(newcounter) - int(fuc_last) os.system("echo "+begintime+" getAclCounter "+fuc_ip + '-' +fuc_acl+'-'+ fuc_rule+'-'+ newcounter+" >> "+pythonlog) # log to mylog.txt #write to tmp file--- iofile = open(devicetmp,'a') write_line = fuc_ip + '\t' +fuc_acl+'\t'+ fuc_rule+'\t'+fuc_description +'\t'+ newcounter+'\t'+fuc_muti_mail+'\t'+fuc_muti_phone+'\t'+fuc_fazhi+'\n' iofile.write(write_line) iofile.close() #charge status to send mail if changenum > int(fuc_fazhi): #print str(int(newcounter) - int(fuc_last)) sendtime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) os.system("echo "+sendtime+" getAclCounter "+fuc_ip + '-' +fuc_acl+'-'+ fuc_rule+'-'+ str(changenum)+"- -! >> "+pythonlog) # log to mylog.txt sms_str = "ACL Warning:"+fuc_description+" counter up to "+str(changenum) +" !" #sms_str = " IP:"+fuc_ip+" acl:"+fuc_description+" counter up to "+str(changenum) +" warning!" mail_str = " IP:"+fuc_ip+"</p> acl:"+fuc_description+"</p>counter up to "+str(changenum) +" warning!" messageMode.send_muti_sms(fuc_muti_phone,sms_off,'getAclCounter sms ',sms_str) messageMode.sendtxtmail('GetAclCounter',mail_off,mail_str,fuc_muti_mail,sendtime) return 'func ok'
#!/usr/bin/python #coding=utf-8 import time import messageMode muti_phone = '13521161889' muti_mail = '*****@*****.**' arg_msg = 'baby,Be careful on the road' begintime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) messageMode.send_muti_sms(muti_phone, 0, 'hello baby', arg_msg) messageMode.sendtxtmail('becare baby', 0, arg_msg, muti_mail, begintime) print 'ok'
os.makedirs(allarpmacbakpath) filenametime = time.strftime('%Y%m%d%H%M%S.arpmacbak', time.localtime(time.time())) filenametimearp = time.strftime('%Y%m%d%H%M%S.arpbak', time.localtime(time.time())) filenametimemac = time.strftime('%Y%m%d%H%M%S.macbak', time.localtime(time.time())) os.system("cp " + allarpmacfile + " " + allarpmacfilebakpath + '/' + filenametime) os.system("cp " + allarpfile + " " + allarpmacbakpath + '/' + filenametimearp) os.system("cp " + allmacfile + " " + allarpmacbakpath + '/' + filenametimemac) os.system( "echo " + midtime + " creat new allarpmacfile,allarp,allmac ok and backup ready ! >> " + pythonlog) # log to mylog.txt endtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) print 'begintime:', begintime print 'endtime:', endtime os.system("echo " + endtime + " getMacArp successful check allarpmac.html ! >> " + pythonlog) # log to mylog.txt sendtxt = 'arp_count:' + str( arpid ) + '</p>' + 'mac_count:' + mac_count + '</p>' + 'arp->if success:' + str( ifcount) + '</p>' + 'know arp,but no interface:' + str( noifcount) + '</p>' messageMode.sendtxtmail('getMacArp ready!', 0, sendtxt, receiver, endtime)
def alllogscan(_fuc_ip, _fuc_name, _fuc_muti_grep, _fuc_muti_mail, _fuc_muti_phone): '''log scan ''' successful_flag = 'ok' mail_tmp_list = [] newlogfile = '' newlogfile_bak = '' #print _fuc_ip,_fuc_name,_fuc_muti_grep,_fuc_muti_mail,_fuc_muti_phone log_path = logfile_path + _fuc_ip log_temp_path = alllogscan_path + _fuc_ip + '.logtmp' sendmail_temp_path = alllogscan_path + _fuc_ip + '.lasttmp' #--------read last mail to mail_tmp_list-------------- if os.path.exists(sendmail_temp_path): last_mail = open(sendmail_temp_path) for lastmailline in last_mail.readlines(): mail_tmp_list.append(lastmailline) last_mail.close() print 'last mail line', len(mail_tmp_list) #-------creat new grep log to ip.logtmp------------ for grep_index in range(0, len(_fuc_muti_grep.split(';'))): every_grep = _fuc_muti_grep.split(';')[grep_index] every_grep = every_grep.replace('_', ' ') commandstr_os = 'tail -n ' + str( once_line ) + ' ' + log_path + ' |egrep ' + every_grep + ' >> ' + log_temp_path print commandstr_os os.system(commandstr_os) #-----read the file ip.logtmp to MEM------ Tmp_log_mem = open(log_temp_path) for line in Tmp_log_mem.readlines(): newlogfile += line Tmp_log_mem.close() newlogfile_bak = newlogfile print 'newlog lines:', newlogfile_bak.count('\n') #----delete every line of last mail in this newlogfile--find different content-- for index_last in range(0, len(mail_tmp_list)): #print index_last #print newlogfile_bak.find(every_line) newlogfile_bak = newlogfile_bak.replace(mail_tmp_list[index_last], '') print 'diff lines:', newlogfile_bak.count('\n') print 'diff content will be send:', newlogfile_bak #-----write to lastlogtmp ------ thistime_mail = open(sendmail_temp_path, 'w') thistime_mail.write(newlogfile) thistime_mail.close() if newlogfile_bak != '': #print 'mail send ----------' #print _fuc_ip,_fuc_name,_fuc_muti_grep,_fuc_muti_mail,_fuc_muti_phone #-------send mail to receivers---------------- messageMode.send_muti_sms(_fuc_muti_phone, sms_off, 'Alllogscan ' + _fuc_name, 'log:' + _fuc_name + ' ' + newlogfile_bak) messageMode.sendtxtmail(_fuc_name + ' ' + ' log scan', mail_off, newlogfile_bak, _fuc_muti_mail, begintime) return successful_flag
for index in range(0, allids_2): func_ret = func(index, allarpmacfile_2) if str(func_ret).find('error') == -1: #print func_ret line_list.append(func_ret) allline = len(line_list) print 'push to cmdb all line count:', allline temp_list = 'Data=' + '{' + 'infoList' + ':' + str(line_list) + '}' print "hong->->", temp_list u = urllib.urlopen(API_url, temp_list) cmdb_ret = u.read() print cmdb_ret cmdb_result = str(cmdb_ret)[11:12] if int(cmdb_result) == 1: print 'push to API successful!' #os.system("echo "+begintime+" push to API successful! >> "+pythonlog) else: print 'push to API error!' os.system("echo " + begintime + " push to API error - -! >> " + pythonlog) # result report messageMode.sendtxtmail( str(allline) + " Udt data Push to API", 0, 'push to cmdb successful!', receiver, begintime) endtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) os.system("echo " + endtime + " udt_pushto_API over ! >> " + pythonlog) # log to mylog.txt