|
| 1 | +#!/usr/bin/python3 |
| 2 | +import requests,re |
| 3 | +import os,sys,time,math |
| 4 | +import time |
| 5 | +from subprocess import Popen, PIPE |
| 6 | +def send2(text = "ahoj"): |
| 7 | + headers = { 'Content-Type':'application/json',} |
| 8 | + data = { |
| 9 | + 'text':text, |
| 10 | + 'username':'alicetrg', |
| 11 | + } |
| 12 | + response = requests.post('https://mattermost.web.cern.ch/hooks/75949oimoinr9b47uhp8c1oomh',headers=headers,json=data) |
| 13 | + print(response) |
| 14 | +# |
| 15 | +def send(): |
| 16 | + """ |
| 17 | + does not work always return can not parse data |
| 18 | + """ |
| 19 | + Hpar = '\\"Content-Type:application/json\\"' |
| 20 | + texth = '\\"text\\"' |
| 21 | + text = '\\"lalal\\"' |
| 22 | + usernameh = '\\"username\\"' |
| 23 | + username = '\\"alicetrg\\"' |
| 24 | + dpar = '{}:{},{}:{}'.format(texth,text,usernameh,username) |
| 25 | + dpar = "{"+dpar+"}" |
| 26 | + dpar = '"'+dpar+'"' |
| 27 | + #print(dpar) |
| 28 | + cmd = 'curl -g -i -X POST -H {} -d {} https://mattermost.web.cern.ch/hooks/75949oimoinr9b47uhp8c1oomh'.format(Hpar,dpar) |
| 29 | + #cmd = 'curl -i -X POST -H {} https://mattermost.web.cern.ch/hooks/75949oimoinr9b47uhp8c1oomh'.format(Hpar) |
| 30 | + #cmd='curl -s -o /dev/null {} -F blob=@datafile.root {}/CTP/Calib/{}/{}/{}'.format(wpar, ccdb, actid, tval_s, tval_e) |
| 31 | + process = Popen(cmd.split(), stdout = PIPE, stderr = PIPE) |
| 32 | + stdo, stde = process.communicate() |
| 33 | + print(cmd) |
| 34 | + print(stdo) |
| 35 | + print("====") |
| 36 | + print(stde) |
| 37 | +def getLog(service): |
| 38 | + #cmd = "journalctl --no-hostname --user-unit "+service |
| 39 | + cmd = 'journalctl --no-hostname --user-unit --since "1 day ago" '+service |
| 40 | + print(cmd,cmd.split()) |
| 41 | + process = Popen(cmd.split(), stdout = PIPE, stderr = PIPE) |
| 42 | + stdo, stde= process.communicate() |
| 43 | + stdo_str = stdo.decode("utf-8") |
| 44 | + if stdo_str.find("No entries") != -1: |
| 45 | + print("No entries for service:",service) |
| 46 | + return None |
| 47 | + #print(stdo) |
| 48 | + return stdo_str |
| 49 | +NMAX = 3 |
| 50 | +def parseLog(log): |
| 51 | + nsent = 0 |
| 52 | + lines = log.split('\n') |
| 53 | + print("# lines:",len(lines)) |
| 54 | + print(lines[len(lines)-2]) |
| 55 | + for line in lines: |
| 56 | + if line.find("ERROR") != -1: |
| 57 | + print(line) |
| 58 | + if nsent < NMAX: |
| 59 | + send2(line) |
| 60 | + nsent += 1 |
| 61 | + if line.find("ALARM") != -1: |
| 62 | + print(line) |
| 63 | + if nsent < NMAX: |
| 64 | + send2(line) |
| 65 | + nsent += 1 |
| 66 | +# |
| 67 | +nWarn = 0 |
| 68 | +nAlarm = 0 |
| 69 | +nError = 0 |
| 70 | +def printNew(list,n,send = 0): |
| 71 | + print(n,len(list)) |
| 72 | + sendnow = 0 |
| 73 | + if len(list) > n : |
| 74 | + sendnow = 1 |
| 75 | + for i in list[n:]: |
| 76 | + print(i) |
| 77 | + n = len(list) |
| 78 | + if sendnow and send: |
| 79 | + print("sending to mm:",line) |
| 80 | + send2(line) |
| 81 | + return n |
| 82 | +def getLogFile(): |
| 83 | + global nWarn, nAlarm,nError |
| 84 | + nWarnList = [] |
| 85 | + nAlarmList = [] |
| 86 | + nErrorList = [] |
| 87 | + MAX = 3 |
| 88 | + file = "/home/rl/WORK/ctpproxy110424.log" |
| 89 | + f = open(file,"r") |
| 90 | + nsent = 0 |
| 91 | + for line in f: |
| 92 | + if line.find("ERROR") != -1: |
| 93 | + print(line) |
| 94 | + if line.find("ALARM") != -1: |
| 95 | + #print(line) |
| 96 | + nAlarmList.append(line) |
| 97 | + #send2(line) |
| 98 | + #nsent += 1 |
| 99 | + if line.find("WARN") != -1: |
| 100 | + #print(line) |
| 101 | + #items = line.split("\]\[") |
| 102 | + #items = re.split('\[|\]',line) |
| 103 | + nWarnList.append(line) |
| 104 | + #print(nWarn) |
| 105 | + nWarn = printNew(nWarnList,nWarn) |
| 106 | + nAlarm = printNew(nAlarmList,nAlarm,1) |
| 107 | + f.close() |
| 108 | +if __name__ =="__main__": |
| 109 | + #send2("uj0"); |
| 110 | + while 1: |
| 111 | + now = time.localtime() |
| 112 | + current_time = time.strftime("%H:%M:%S",now) |
| 113 | + print("===> Time =", current_time) |
| 114 | + log = getLogFile() |
| 115 | + time.sleep(15) |
| 116 | + |
| 117 | + #parseLog(log) |
0 commit comments