Manage machine groups with Python SDK
Create, modify, query, and delete machine groups in Simple Log Service by using the SDK for Java. Sample code is provided for each operation.
Prerequisites
Simple Log Service is activated.
Simple Log Service SDK for Python is initialized.
Simple Log Service SDK for Python is installed. For more information, see Install Simple Log Service SDK for Python.
Simple Log Service is activated. For more information, see Enable Log Service.
Precautions
In this example, the public Simple Log Service endpoint for the China (Hangzhou) region is used. Endpoint: https://cn-hangzhou.log.aliyuncs.com.
If you want to access Simple Log Service from other Alibaba Cloud services that reside in the same region as your project, you can use the internal Simple Log Service endpoint, which is https://cn-hangzhou-intranet.log.aliyuncs.com.
For more information about the supported regions and endpoints of Simple Log Service, see Endpoint.
Create a machine group
The following example creates an IP address-based machine group named ali-test-machinegroup.
from aliyun.log import LogClient, MachineGroupDetail
import os
# This example retrieves the AccessKey ID and AccessKey Secret from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The Log Service endpoint. This example uses the China (Hangzhou) endpoint. Replace it with your actual endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)
# The name of an existing project. Replace the value with your actual project name.
project_name = "ali-test-project"
if __name__ == '__main__':
print("ready to create machinegroup")
# The machine group configuration.
my_json_str = {
"machine_list": ["47.100.XX.XX", "47.100.XX.XX"],
"machine_type": "ip",
"group_type": "",
"group_name": "ali-test-machinegroup",
"group_attribute": {
"groupTopic": ""
}
}
# Create a machine group configuration object.
machine_group_detail = MachineGroupDetail()
# Read the machine group configuration from the JSON string.
machine_group_detail.from_json(my_json_str)
# Create the machine group.
res1 = client.create_machine_group(project_name, machine_group_detail)
print("create machinegroup success") Expected output:
ready to create machinegroup
create machinegroup successUpdate a machine group
You can also manage machine groups in the Log Service console.
The following example updates the configuration of a machine group.
from aliyun.log import LogClient, MachineGroupDetail
import os
# This example retrieves the AccessKey ID and AccessKey Secret from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The Log Service endpoint. This example uses the China (Hangzhou) endpoint. Replace it with your actual endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)
# The name of an existing project. Replace the value with your actual project name.
project_name = "ali-test-project"
# The name of an existing machine group. Replace the value with your actual machine group name.
group_name = "ali-test-machinegroup"
if __name__ == '__main__':
print("ready to update machinegroup")
# Query the configuration of the machine group to be updated.
print("Before update, the machine list is:")
res = client.get_machine_group(project_name, group_name)
print(res.get_machine_group().machine_list)
# The updated machine group configuration.
# This example adds a server to the machine_list.
my_json_str = {
"machine_list": ["47.100.XX.XX", "47.100.XX.XX", "47.100.XX.XX"],
"machine_type": "ip",
"group_type": "",
"group_name": "ali-test-machinegroup",
"group_attribute": {
"groupTopic": ""
}
}
# Create a machine group configuration object.
machine_group_detail = MachineGroupDetail()
# Read the updated configuration from the JSON string.
machine_group_detail.from_json(my_json_str)
# Update the machine group.
client.update_machine_group(project_name, machine_group_detail)
print("After update, the machine list is:")
res2 = client.get_machine_group(project_name, group_name)
print(res2.get_machine_group().machine_list)
print("update machinegroup success")Expected output:
ready to update machinegroup
Before update, the machine list is:
['47.100.XX.XX', '47.100.XX.XX']
After update, the machine list is:
['47.100.XX.XX', '47.100.XX.XX', '47.100.XX.XX']
update machinegroup successList machine groups
The following example lists all machine groups in a project.
from aliyun.log import LogClient, MachineGroupDetail
import os
# This example retrieves the AccessKey ID and AccessKey Secret from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The Log Service endpoint. This example uses the China (Hangzhou) endpoint. Replace it with your actual endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)
# The name of an existing project. Replace the value with your actual project name.
project_name = "ali-test-project"
if __name__ == '__main__':
print("ready to list machinegroup")
# List the machine groups. The '0' and '100' specify the offset and the number of machine groups to return, respectively.
res = client.list_machine_group(project_name, 0, 100)
for machine_group in res.get_machine_group():
print(machine_group)
print("list machinegroup success")Expected output:
ready to list machinegroup
ali-test-machinegroup
ali-test-machinegroup2
ali-test-machinegroup3
list machinegroup successDelete a machine group
The following example deletes a machine group.
from aliyun.log import LogClient, MachineGroupDetail
import os
# This example retrieves the AccessKey ID and AccessKey Secret from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The Log Service endpoint. This example uses the China (Hangzhou) endpoint. Replace it with your actual endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)
# The name of an existing project. Replace the value with your actual project name.
project_name = "ali-test-project"
# The name of an existing machine group. Replace the value with your actual machine group name.
group_name = "ali-test-machinegroup"
if __name__ == '__main__':
print("ready to delete machinegroup")
# Delete the specified machine group.
client.delete_machine_group(project_name, group_name)
print("delete machinegroup success")Expected output:
ready to delete machinegroup
delete machinegroup successGet a machine group
The following example queries the configuration of a machine group.
from aliyun.log import LogClient, MachineGroupDetail
import os
# This example retrieves the AccessKey ID and AccessKey Secret from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The Log Service endpoint. This example uses the China (Hangzhou) endpoint. Replace it with your actual endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)
# The name of an existing project. Replace the value with your actual project name.
project_name = "ali-test-project"
# The name of an existing machine group. Replace the value with your actual machine group name.
group_name = "ali-test-machinegroup"
if __name__ == '__main__':
print("ready to get machinegroup")
# Get the configuration of the specified machine group.
res = client.get_machine_group(project_name, group_name)
print(res.get_machine_group().to_json())
print("get machinegroup success")Expected output:
ready to get machinegroup
{'groupName': 'ali-test-machinegroup', 'groupType': '', 'groupAttribute': {'externalName': '', 'groupTopic': '', 'osType': '', 'region': '', 'policy': ''}, 'machineIdentifyType': 'ip', 'machineList': ['47.100.XX.XX', '47.100.XX.XX']}
get machinegroup successApply a Logtail configuration to a machine group
You can also manage Logtail configurations in the Log Service console.
The following example applies a Logtail configuration to a machine group.
from aliyun.log import LogClient, MachineGroupDetail
import os
# This example retrieves the AccessKey ID and AccessKey Secret from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The Log Service endpoint. This example uses the China (Hangzhou) endpoint. Replace it with your actual endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)
# The name of an existing project. Replace the value with your actual project name.
project_name = "ali-test-project"
# The name of an existing machine group. Replace the value with your actual machine group name.
group_name = "ali-test-machinegroup"
# The name of an existing Logtail configuration. Replace the value with your actual configuration name.
config_name = "ali-test-logtailconfig"
if __name__ == '__main__':
print("ready to apply config to machinegroup")
# Apply the Logtail configuration to the machine group.
client.apply_config_to_machine_group(project_name, config_name, group_name)
print("apply config to machinegroup success")Expected output:
ready to apply config to machinegroup
apply config to machinegroup successReferences
In addition to its native SDK, SLS also supports the common Alibaba Cloud SDKs. For more information, see Log Service_SDKcenter-Alibaba CloudOpenAPIDeveloper Portal.