123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- const Ecs20140526 = require('@alicloud/ecs20140526').default
- const $Ecs20140526 = require('@alicloud/ecs20140526')
- const OpenApi = require('@alicloud/openapi-client').default;
- const $OpenApi = require('@alicloud/openapi-client');
- const $tea = require('@alicloud/tea-typescript')
- const STATUS = {
- /**
- * 等待状态
- */
- pending: "pending",
- /**
- * 完成状态
- */
- fulfilled: "fulfilled"
- }
- function getLocalTimeToUTC() {
- var date = new Date()
- date.setMilliseconds(0)
- date.setMinutes(date.getMinutes() + date.getTimezoneOffset() + 3)
- return date
- }
- function dateFormat(fmt, date) {
- let ret;
- const opt = {
- "Y+": date.getFullYear().toString(), // 年
- "m+": (date.getMonth() + 1).toString(), // 月
- "d+": date.getDate().toString(), // 日
- "H+": date.getHours().toString(), // 时
- "M+": date.getMinutes().toString(), // 分
- "S+": date.getSeconds().toString() // 秒
- // 有其他格式化字符需求可以继续添加,必须转化成字符串
- };
- for (let k in opt) {
- ret = new RegExp("(" + k + ")").exec(fmt);
- if (ret) {
- fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
- };
- };
- return fmt;
- }
- class Client {
- /**
- * 使用AK&SK初始化账号Client
- * @param accessKeyId
- * @param accessKeySecret
- * @return Client
- * @throws Exception
- */
- static createClient(accessKeyId, accessKeySecret) {
- let config = new $OpenApi.Config({
- // 您的AccessKey ID
- accessKeyId: accessKeyId,
- // 您的AccessKey Secret
- accessKeySecret: accessKeySecret,
- });
- // 访问的域名
- config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
- return new Ecs20140526(config);
- }
- constructor() {
- this.band = 0
- this.status = STATUS.fulfilled;
- this.client = Client.createClient("LTAIUrvuHqj8pvry", "JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4")
- // 看房服务器
- this.instanceId = 'i-wz9g1c3fleilk1n9q5uu'
- this.describeInstances()
- setInterval(() => {
- this.describeInstances()
- }, 15000);
- }
- /**
- * 查看实例详细信息
- */
- async describeInstances() {
- //https://help.aliyun.com/document_detail/25506.html#h2-url-4
- try {
- let describeInstancesRequest = new $Ecs20140526.DescribeInstancesRequest({
- regionId: "cn-shenzhen",
- instanceIds: "[\"" + this.instanceId + "\"]",
- });
- let response = await this.client.describeInstances(describeInstancesRequest);
- if (response.body.totalCount > 0) {
- if (response.body.instances.instance[0]) {
- let band = response.body.instances.instance[0].internetMaxBandwidthOut
- if (band) {
- if (this.band) {
- if (this.band != band) {
- if (this.band < band) {
- console.log("带宽已升级:" + band + "MB")
- } else if (this.band > band) {
- console.log("带宽已变化:" + band + "MB")
- }
- } else {
- console.log('当前带宽:'+ band + "MB",'连接数:'+(this.conns || 0))
- }
- // 当带宽有变化时重置为初始状态
- this.status = STATUS.fulfilled
- }
- if(!this.band){
- console.log("带宽初始值:" + band + "MB")
- }
- this.band = band
- }
- }
- }
- } catch (error) {
- console.log("获取带宽出错:", error)
- }
- }
- async modifyInstanceNetwork(band) {
- //https://help.aliyun.com/document_detail/25545.htm?spm=a2c4g.11186623.0.0.1ec05d7aOys0wB#t9937.html
- if (!band || band > 100 || this.band >= 100 || band<=this.band) {
- return
- }
- console.log("开始升级带宽:"+band)
- this.status = STATUS.pending
- try {
- var utc = getLocalTimeToUTC()
- var startTime = dateFormat('YYYY-mm-ddTHH:MMZ', utc)
- utc.setHours(utc.getHours() + 4)
- var endTime = dateFormat('YYYY-mm-ddTHHZ', utc)
- console.log(startTime, endTime)
- let modifyInstanceNetworkSpecRequest = new $Ecs20140526.ModifyInstanceNetworkSpecRequest({
- instanceId: this.instanceId,
- internetMaxBandwidthOut: band,
- startTime: startTime,
- endTime: endTime,
- autoPay: true,
- });
- let response = await this.client.modifyInstanceNetworkSpec(modifyInstanceNetworkSpecRequest);
- console.log(response)
- } catch (error) {
- this.status = STATUS.fulfilled
- console.log("升级带宽出错:", error)
- }
- }
- async autoUpdate(conns) {
- this.conns = conns
- if (this.status == STATUS.pending) {
- return
- }
- if (conns >= 300 && conns < 800) {
- await this.modifyInstanceNetwork(50)
- } else if (conns >= 800 && conns < 1500) {
- await this.modifyInstanceNetwork(75)
- } else if (conns > 1500) {
- await this.modifyInstanceNetwork(100)
- }
- }
- }
- const client = new Client()
- module.exports = client
|