(1)首先需要在項目的根路徑下,創(chuàng)建utils包,用于存放項目的其他數(shù)據(jù)接口;
在安仁等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站制作、網(wǎng)站設(shè)計 網(wǎng)站設(shè)計制作按需規(guī)劃網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都營銷網(wǎng)站建設(shè),成都外貿(mào)網(wǎng)站建設(shè)公司,安仁網(wǎng)站建設(shè)費用合理。(2)然后在utils包下,創(chuàng)建一個
wechat_send.py
模塊, 用于給用戶封裝企業(yè)微信的接口,提供給外界調(diào)用;
import requests
import json
class Wechat_Info:
def __init__(self):
self.partyID = '1'
self.corpID = 'ww5cfabaf35ce8cd7b'
self.secret = 'uiwvmNj8f1IVy3QYrZ62WePGFKA_BsIPmHigq3TRydM'
self.agentID = '1000002'
self.token = None
def __get_token(self, corpid, secret):
Url = "/tupian/20230522/gettoken"
Data = {
"corpid": corpid,
"corpsecret": secret
}
r = requests.get(url=Url, params=Data)
token = r.json()['access_token']
return token
def send_message(self, message):
url = "/tupian/20230522/send
self.__get_token(self.corpID, self.secret))
data = {
"toparty": self.partyID,
"msgtype": "text",
"agentid": self.agentID,
"text": {
"content": message
},
"safe": "0"
}
result = requests.post(url=url, data=json.dumps(data))
return result.text
if __name__ == '__main__':
wechat_info = Wechat_Info()
result = wechat_info.send_message('微信測試')
print(result)
partyID
、
corpID
、
secret
、
agentID
都是配置信息,需要引入一個讀取配置文件的庫
configparser
;
安裝:
pip install configparser
xkd.conf
文件;
[Wechat]
party_id = 1
corp_id = ww5cfabaf35ce8cd7b
secret = uiwvmNj8f1IVy3QYrZ62WePGFKA_BsIPmHigq3TRydM
agent_id = 1000002
const_file.py
文件,依賴作為項目中全局常量的引用;
import os
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
CONFIG_DIR = os.path.join(PROJECT_DIR, 'cfg')
# 指定配置文件的路徑
CONFIG_FILE = os.path.join(CONFIG_DIR, 'xkd.conf')
wechat_send.py
模塊接口(配置中的信息都是明文的,在項目開發(fā)中,最好將重要的配置信息進行加密,可以使用base64進行加密和解密);
import requests
import json
from configparser import ConfigParser
from utils.const_file import CONFIG_FILE
class Wechat_Info:
def __init__(self):
self.wechat_info = self.__get_config_info()
def __get_config_info(self):
parser = ConfigParser()
parser.read(CONFIG_FILE)
wechat_info = {}
wechat_info['party_id'] = parser.get('Wechat', 'party_id')
wechat_info['corp_id'] = parser.get('Wechat', 'corp_id')
wechat_info['secret'] = parser.get('Wechat', 'secret')
wechat_info['agent_id'] = parser.get('Wechat', 'agent_id')
return wechat_info
def __get_token(self, corpid, secret):
Url = "/tupian/20230522/gettoken"
Data = {
"corpid": corpid,
"corpsecret": secret
}
r = requests.get(url=Url, params=Data)
token = r.json()['access_token']
return token
def send_message(self, message):
url = "/tupian/20230522/send
self.__get_token(self.wechat_info.get('corp_id'), self.wechat_info.get('secret')))
data = {
"toparty": self.wechat_info.get('party_id'),
"msgtype": "text",
"agentid": self.wechat_info.get('agent_id'),
"text": {
"content": message
},
"safe": "0"
}
result = requests.post(url=url, data=json.dumps(data))
return result.text
if __name__ == '__main__':
wechat_info = Wechat_Info()
result = wechat_info.send_message('微信測試')
print(result)
(7)在utils包下,創(chuàng)建
aliyun_monitor.py
文件,封裝阿里云的云監(jiān)控數(shù)據(jù)的接口;
(8)然后編輯xkd.conf配置文件,添加阿里云
RAM access key
和
access key secret
、
region id
;
[Wechat]
party_id = 1
corp_id = wwabdfbc8feeee095a
secret = NY2nQE1cavlug_IuyMoRlIZDIOdsDS4KMpo_81XWslo
agent_id = 1000002
[ALIYUN]
access_key = LTAIxdvbuz2MD74Z
access_key_secret = I2b7O1UJmkjfgHVC4i7Atb8gGXwAbU
region_id = cn-shenzhen
aliyun_monitor.py
文件;
from aliyunsdkcore import client
from aliyunsdkcms.request.v20180308 import QueryMetricListRequest
from datetime import datetime, timedelta
import json
from configparser import ConfigParser
from utils.const_file import CONFIG_FILE
class CloudMonitor:
def __init__(self):
self.ram_info = self.__get_ram_info()
self.clt = client.AcsClient(self.ram_info.get('access_key'), self.ram_info.get('access_key_secret'), self.ram_info.get('region_id'))
self.request = QueryMetricListRequest.QueryMetricListRequest()
self.last_average = None
def __get_ram_info(self):
parser = ConfigParser()
parser.read(CONFIG_FILE)
ram_info = {}
ram_info['access_key'] = parser.get('ALIYUN', 'access_key')
ram_info['access_key_secret'] = parser.get('ALIYUN', 'access_key_secret')
ram_info['region_id'] = parser.get('ALIYUN', 'region_id')
return ram_info
def __get_start_timestamp(self):
start_datetime = datetime.now() - timedelta(minutes=3)
start_timestamp = int(start_datetime.timestamp() * 1000)
return start_timestamp
def get_last_metric(self,instance_id, monitor_key):
'''
:param instance_id: ECS是實例ID
:param monitor_key: 需要監(jiān)控的指標(biāo)
:return:
'''
self.request.set_accept_format('json')
self.request.set_Project('acs_ecs_dashboard')
self.request.set_Metric('{}'.format(monitor_key))
start_time = self.__get_start_timestamp()
self.request.set_StartTime(start_time)
self.request.set_EndTime(int(datetime.now().timestamp() * 1000))
self.request.set_Dimensions("{'instanceId': '%s'}" % (instance_id))
result = self.clt.do_action_with_exception(self.request)
result = json.loads(result)
data_str = result.get('Datapoints')
data_list = json.loads(data_str)
return data_list[-1]['Average']
if __name__ == '__main__':
monitor = CloudMonitor()
result = monitor.get_last_metric('i-wz98bynewgl7gu3jqqb3', 'CPUUtilization')
print(result)
pip install django-crontab
django-crontab
到INSTALLED_APPS;
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users',
'courses',
'teachers',
'schools',
'rest_framework',
'django_crontab',
]
import CloudMonitor
和
Wechat_Info
;
from utils.aliyun_monitor import CloudMonitor
from utils.wechat_send import Wechat_Info
def send_aliyun_metric_to_wechat():
monitor = CloudMonitor()
wechat_info = Wechat_Info()
result = monitor.get_last_metric('i-wz98bynewgl7gu3jqqb3', 'CPUUtilization')
wechat_info.send_message('當(dāng)前服務(wù)器CPU使用率是:{}'.format(result))
CRONJOBS = [
('* * * * *', 'users.cron.send_aliyun_metric_to_wechat')
]
python manage.py crontab add
;
安裝命令行:
pip install configparser
;
ConfigParser是用來讀取配置文件的模塊,配置文件的格式跟windows下的ini配置文件相似,可以包含一個或多個section,每個section都有自己的option;
section 用 [sect_name] 表示,每個option是一個鍵值對,使用分隔符
=
或
:
隔開;
在 option 中分隔符兩端的空格會被忽略掉;
配置文件使用可以使用
#
進行注釋;
安裝命令:
pip install django-crontab
django-crontab使用前需要添加到settings文件中的INSTALLED_APPS中;
添加定時任務(wù):
python manage.py crontab add
,注意如果修改了任務(wù),也需要再次運行此命令;
清除定時任務(wù):
python manage.py crontab remove
;
顯示定時任務(wù):
python manage.py crontab show
;
注意
django-crontab
必須在Linux的crontab開啟的情況下方可使用,不然會出現(xiàn)不執(zhí)行的情況;
參考: /tupian/20230522/login.html
新聞標(biāo)題:將獲取的指標(biāo)封裝成接口提供API請求調(diào)用-創(chuàng)新互聯(lián)
本文鏈接:http://jinyejixie.com/article16/dhojgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、建站公司、App開發(fā)、商城網(wǎng)站、用戶體驗、網(wǎng)站設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容