获取rds的cpu和内存使用情况

Posted hxlasky

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取rds的cpu和内存使用情况相关的知识,希望对你有一定的参考价值。

 

python版本:2.7.12

#!/usr/bin/env python
#coding=utf-8

import time
import os, json, urllib, datetime, shutil
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkrds.request.v20140815.DescribeDBInstancePerformanceRequest import DescribeDBInstancePerformanceRequest

now_time = datetime.datetime.now()
yes_time = now_time + datetime.timedelta(days = -1)
key_item="mysql_MemCpuUsage"
logfilepath="/home/oralce/scripts/monitor_script/logdata/"
instance_list=["1","2","3","4","5","6"]


##获取rds cpu数据并写入文件
def get_cpudata(instance_id,logfile_name):
        client = AcsClient(xxxxx,xxxxx,xxxxx)
        request = DescribeDBInstancePerformanceRequest()
        request.set_accept_format(json)
        start_time = datetime.datetime.strftime(yes_time,%Y-%m-%d) + T16:00Z
        end_time   = datetime.datetime.strftime(now_time,%Y-%m-%d) + T16:00Z
        request.set_StartTime(start_time)
        request.set_EndTime(end_time)
        ##获取cpu和内存数据
        request.set_Key(key_item)
        request.set_DBInstanceId(instance_id)
        response = client.do_action_with_exception(request)
        result=json.loads(response)

        list = result[PerformanceKeys][PerformanceKey][0][Values][PerformanceValue];
        ##取最新一条数据
        list_length=len(list)
        last_row=list[list_length-1]
        check_utc_date =last_row[Date]

        ##utc时间转换北京时间
        check_date=utc2local(datetime.datetime.strptime(check_utc_date, "%Y-%m-%dT%H:%M:%SZ"))
        check_value = last_row[Value]

        ##数据拼接
        cpu_data=instance_id + | + datetime.datetime.strftime(check_date,%Y-%m-%d %H:%M:%S) + | + check_value.replace("&","|")
        
        logfile=logfilepath + logfile_name
        ##写入文件
        with open(logfile, mode=a) as filename:
                filename.write(cpu_data)
                filename.write(\n)


##UTC时间转换成本地时间,参数为datetime类型
def utc2local(utc_st):
    now_stamp = time.time()
    local_time = datetime.datetime.fromtimestamp(now_stamp)
    utc_time = datetime.datetime.utcfromtimestamp(now_stamp)
    offset = local_time - utc_time
    local_st = utc_st + offset
    return local_st


if __name__ == __main__:
        for instance_id in instance_list:
            ##1实例
            if instance_id =="1" :
                logfile="1_cpu.log"
            ##2实例
            if instance_id =="2" :
                logfile="2_cpu.log"
            ##3实例
            if instance_id =="3" :
                logfile="3_cpu.log"
            ##4实例
            if instance_id =="4" :
                logfile="4_cpu.log"
            ##5实例
            if instance_id =="5" :
                logfile="5_cpu.log"
            ##6实例
            if instance_id =="6" :
                logfile="6_cpu.log"
            
            get_cpudata(instance_id,logfile)

 

以上是关于获取rds的cpu和内存使用情况的主要内容,如果未能解决你的问题,请参考以下文章

获取进程的 CPU 和内存使用情况的正确性能计数器是啥?

Prometheus 查询以获取 kubernetes pod 中的 CPU 和内存使用情况

获取进程的CPU和内存使用情况的正确性能计数器是什么?

获取cpu和内存使用情况

Python 系统资源信息获取CPU 和内存使用情况

PHP如何在windows下获取计算机的cpu和内存