Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to upload logs and monitor alarms

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use Python to upload logs and monitor alarms". In daily operation, I believe many people have doubts about how to use Python to upload logs and monitor alarms. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to upload logs and monitor alarms using Python". Next, please follow the editor to study!

1. Prepare for

Before you begin, make sure that Python and pip are successfully installed on your computer. If not, you can visit this article: hyperdetailed Python installation Guide for installation.

(optional 1) if you use Python for data analysis, you can install Anaconda directly, which comes with Python and pip.

(optional 2) in addition, it is recommended to use VSCode editor, which has many advantages

Please choose any of the following ways to enter the command to install dependencies:

1. Windows environment opens Cmd (start-run-CMD).

2. Open Terminal in MacOS environment (enter Terminal in command+ space).

3. If you are using a VSCode editor or Pycharm, you can use Terminal directly at the bottom of the interface.

Pip install aliyun-log-python-sdk

Next, log in to the Ali Cloud console, enter the log application, and use the following steps to create log Project and Logstore:

Click Python-SDK to write, and then create Project and Logstore according to your needs:

Then you will go to this page and click OK directly:

two。 Upload Python logs using Aliyun SDK

In order to upload logs using Aliyun SDK, we need to obtain Access Token first, move the mouse over the avatar in the upper right corner and click AccessKey to manage:

Then click create AccessKey, enter relevant verification information to get accessKeyId and accessKey:

Write Python code to configure AccessKey and the Project and logstore names you created in the first step:

From aliyun.log import LogClient, PutLogsRequest, LogItem, GetLogsRequest, IndexConfigimport time# configuration AccessKey, service entry, Project name, Logstore name and other related information. # Aliyun access key AccessKey. For more information, see access key. # Aliyun account AccessKey has access to all API, which is very risky. It is strongly recommended that you create and use RAM users for API access or daily operation and maintenance. AccessKeyId = "your AccessKey ID" accessKey = "your AccessKey" # the domain name of the log service. For more information, see Service Portal. Here, take Guangzhou as an example. Please fill in other areas according to the actual situation. Endpoint = "cn-guangzhou.log.aliyuncs.com" # create the log service Client. Client = LogClient (endpoint, accessKeyId, accessKey) # Project name. Project_name = "aliyun-test-project" # Logstore name logstore_name = "aliyun-test-logstore" # query statement. Query = "* | select dev,id from" + logstore_name# from_time and to_time indicate the time range of the query log and the Unix timestamp format. From_time = int (time.time ())-3600to_time = time.time () + 3600

Then we can write Python code to create an index (the index of the log can be understood as a database in MySQL) and insert the log:

# write data to Logstore. Def put_logs (): print ("ready to put logs for% s"% logstore_name) log_group = [] for i in range (0100): log_item = LogItem () contents = [('dev',' test_put'), ('id') Str (I)] log_item.set_contents (contents) log_group.append (log_item) request = PutLogsRequest (project_name, logstore_name, ", log_group, compress=False) client.put_logs (request) print (" put logs for% s success "% logstore_name) time.sleep (5) if _ _ name__ = ='_ _ main__': # writes data to Logstore. Put_logs ()

The corresponding prompt appears after running the program, indicating that the log is uploaded successfully:

Python test.py# ready to put logs for tradingview# put logs for tradingview success

Enter the Project corresponding to the console, and you will see that the log you just uploaded is displayed above:

3. Configure log alarm

The configuration of log alarm is also very simple. Enter your query conditions, get the output and click above to save as alarm:

Add the conditions that you need to monitor and trigger the alarm in the query statistics, for example, I set the alarm to be triggered when the log appears:

The effect is as follows. The text configuration here is too simple, and you can also configure more complex text in the annotations:

It's easy to use. If you have a similar multi-machine log monitoring service, such as distributed model training monitoring, transaction service monitoring, etc., you can consider using this logging service.

At this point, the study on "how to use Python to upload logs and monitor alarms" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report