Best Practices for Backing Up Log Data to OSS (Function)¶
Prerequisites¶
Install Datakit¶
- Click the [Integration] module, [Datakit], and select the appropriate installation command based on your operating system and system type.
-
Copy the Datakit installation command and run it directly on the server that needs to be monitored.
-
Installation directory:
/usr/local/datakit/
- Log directory:
/var/log/datakit/
- Main configuration file:
/usr/local/datakit/conf.d/datakit.conf
- Plugin configuration directory:
/usr/local/datakit/conf.d/
Install Function¶
- Click the [Integration] module, [Funciton], download the installation package, and execute the installation command.
- After installation, access the initialization interface via a browser at
http://Server IP Address:8088
.
- Log in to the system using the default username/password
admin
.
RAM Access Control¶
-
Log in to the RAM console https://ram.console.aliyun.com/users
-
Create a new user: User Management - Users - Create User
-
Save or download the CSV file containing the AccessKey ID and AccessKey Secret (this will be used in the configuration).
-
Authorize the user (grant permissions to manage Object Storage Service (OSS)).
Script Development¶
Log Backup to OSS¶
- Management - Experimental Features - Enable PIP Tool Module
- Install Alibaba Cloud SDK dependency package (oss2)
- Create a new script set and add a script
- Write the code, filling in the AccessKey ID, AccessKey Secret, Bucket Name, and Filename.
Refer to the documentation:
import time
import json
import oss2
@DFF.API('Run DQL via DataKit')
def run_dql_via_datakit():
datakit = DFF.SRC('datakit')
# Use the time_range parameter to limit data from the last 10 minutes
time_range = [
int(time.time() - 600) * 1000,
int(time.time()) * 1000,
]
# DQL query statement
status_code, result = datakit.query(dql='L::`apache`', time_range=time_range, raw=True)
result = json.dumps(result, indent=2)
# Configure OSS information
auth = oss2.Auth('AccessKey ID', 'AccessKey Secret')
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'Bucket Name')
file = bucket.put_object('Filename.txt', result)
- Log in to the OSS console to view the file