Python decorator implements monitoring of abnormal codes

Abnormal, it shouldn’t exist, but we sometimes encounter situations like this. For example, when we monitor the server, we collect information every second, then the information we want is not collected for one second, but the next second is collected. , And then every time we can collect it, but we can’t collect it once. Should we analyze if we can’t collect this time? It can be said that this situation cannot be repeated, and we cannot avoid it because external factors are too great. Too much, we can’t control these external factors, so we will have such a demand, how many times in a period of time, we can display an alarm, or in other words, the frequency of occurrence in a period of time reaches our abnormal permission range We think this is abnormal, and we can send an alarm.

So how do we achieve it? I thought of the decorator. When the program is executed abnormally, I record the time, write to the file, and then read the fifth judgment. The difference between the two timestamps is only less than 60s. Think that this kind of alarm can be sent, if it is longer than 60s, it is considered not enough to send our warning,

So let's take a look at how our code should be written.

import datetime,time,random
def make(func):
    def mak(*args,**kwargs):
        try:
            func(*args,**kwargs)
        except:
            with open('except.txt','a+') as f:
                except_time=datetime.datetime.now()
                f.writelines(except_time.strftime('%Y-%m-%d  %H:%M:%S')+'\n')
                f.close()
            with open('except.txt','rb') as m:
                try:
                    date=m.readlines()[-5].decode('utf-8')
                    ne=(date.split('\r\n')[0])
                    f1=datetime.datetime.strptime(ne,'%Y-%m-%d %H:%M:%S')
                    if (except_time-f1).seconds<6:
                        print('异常!!!fail')
                    else:
                        print('正常!')
                    m.close()
                except:
                    print('越界代表着我们的实验是成功的')
    return mak
@make
def beijing(i,m):
    print(i/m)
if __name__=="__main__":
    while True:
        f=random.choice([0,1,2,3])
        n=random.choice([0,1,2,3])
        beijing(f,n)
        time.sleep(0.3)

In this way, we have realized the abnormal monitoring of a program, let's run the code of this abnormal monitoring. ,
Insert picture description here
We can see that our code can run normally, so let's try it. We monitor the code of multiple programs. Can our script be implemented?

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
@make
def shanghai(i,m):
    print(i/m)
@make
def rizhao(i,m):
    print(i/m)
@make
def zhengzhou(i,m):
    print(i/m)

We added a few methods, and we run them,
Insert picture description here
we can see that as long as there is an exception, our program will record it, of course, this can not be used in our work normally.

Later, you can combine the practice of abnormal monitoring here with the abnormal monitoring script I wrote before.

Guess you like

Origin blog.csdn.net/sinat_38682860/article/details/108488888