把下面的实例ID和secretId、secretKey替换就行了,这里是查询快照然后删除快照并新建一个快照。
由于腾讯云一个轻量只能有两个快照,我这里直接删除第二个快照,所以设置定时任务时,设置多久创建一次新快照都行。
// Depends on tencentcloud-sdk-nodejs version 4.0.3 or higher
const tencentcloud = require("tencentcloud-sdk-nodejs");
const LighthouseClient = tencentcloud.lighthouse.v20200324.Client;
const clientConfig = {
credential: {
secretId: "更改为账号secretId",
secretKey: "更改为账号secretKey",
},
region: "ap-guangzhou",
profile: {
httpProfile: {
endpoint: "lighthouse.tencentcloudapi.com",
},
},
};
const client = new LighthouseClient(clientConfig);
const chakan = {
"Filters": [
{
"Name": "instance-id",
"Values": [
"更改为实例id"
]
}
]
};
client.DescribeSnapshots(chakan).then(
(data) => {
shanchu_id = data.SnapshotSet[1].SnapshotId //数组的第二个
const shanchu = {
"SnapshotIds": [
shanchu_id
]
};
client.DeleteSnapshots(shanchu).then(
(data) => {
console.log('删除一个旧快照成功');
console.log(data);
// 创建快照
const chuangjian = {
"InstanceId": "更改为实例id"
};
client.CreateInstanceSnapshot(chuangjian).then(
(data) => {
console.log('创建一个新快照成功');
console.log(data);
},
(err) => {
console.error("error", err);
}
);
},
(err) => {
console.log('删除一个旧快照失败~~~~必须已有两个快照,才能删除第二个快照并创建新快照');
console.error("error", err);
}
);
},
(err) => {
console.log('查询旧快照失败');
console.error("error", err);
}
);
这里是查询一个轻量的流量使用情况,然后发邮件通知我。这里除了实例id和密钥要替换外,还要设置smtp信息。
// Depends on tencentcloud-sdk-nodejs version 4.0.3 or higher
const tencentcloud = require("tencentcloud-sdk-nodejs");
const LighthouseClient = tencentcloud.lighthouse.v20200324.Client;
const nodemailer = require('nodemailer'); //发送邮件的
function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0 Byte';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
}
function sendEmail(data) {
let transporter = nodemailer.createTransport({
service: 'QQex', // 发送者的邮箱厂商,支持列表:https://nodemailer.com/smtp/well-known/ 如果是qq邮箱,就填QQ, QQex是腾讯企业邮
port: 465, // SMTP 端口
secureConnection: true, // SSL安全链接
auth: { //发送者的账户密码
user: '账户', //账户
pass: 'smtp授权码,到邮箱设置下获取', //smtp授权码,到邮箱设置下获取
}
});
let mailOptions = {
from: '"腾讯云轻量流量通知" <i@zuoridangnian.com>', // 发送者昵称和地址
to: data.email, // 接收者的邮箱地址
subject: data.subject, // 邮件主题
html: data.content
};
//发送邮件
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('邮件发送成功 ID:', info.messageId);
});
}
const clientConfig = {
credential: {
secretId: "更改为账号secretId",
secretKey: "更改为账号secretKey",
},
region: "ap-guangzhou",
profile: {
httpProfile: {
endpoint: "lighthouse.tencentcloudapi.com",
},
},
};
const client = new LighthouseClient(clientConfig);
const params = {
"InstanceIds": [
"更改为实例id"
]
};
let Traffic
client.DescribeInstancesTrafficPackages(params).then(
(data) => {
Traffic = `使用了${bytesToSize(data.InstanceTrafficPackageSet[0].TrafficPackageSet[0].TrafficUsed)},总共有1000GB。`
let mail = {
email: '收件邮箱@qq.com', //收件邮箱
content: `腾讯云轻量应用服务器 流量使用情况:${Traffic}`,
subject: Traffic
}
sendEmail(mail)
},
(err) => {
console.error("error", err);
}
)