Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Debug log messages enabled? (true/false)
var debugMode = process.env.DEBUG || false;
var dryRunMode = process.env.DRYRUN || false;
// --------------------------------------------------------------------------------------
// SECTION: Initialization
// This code sets configuration values and retrieves the server state from a JSON file
// --------------------------------------------------------------------------------------
//
const dataFilePath = './scripts/data.json'
var JSONData = require( dataFilePath); // Reads the JSON data file to get current server state
// Google Stackdriver Monitoring initialization
if (!dryRunMode) var {google} = require('googleapis');
if (!dryRunMode) var monitoring = require('@google-cloud/monitoring');
if (!dryRunMode) var client = new monitoring.MetricServiceClient();
var projectId = "";
var pod_guid = "";
var namespace_name = "";
var zone_name = "";
var cluster_name = "";
var pod_name = "";
// Initialize the JSON file to false and 0 users
var cpuLoadRunning = false;
var customMetricCreated = false;
var userCount = 0;
JSONData.CpuLoadRunning = cpuLoadRunning;
JSONData.customMetricCreated = customMetricCreated;
JSONData.UserCount = userCount;
JSONData.DebugMode = debugMode;
setTimeout(initData, 2000); // Wait for the file to be ready, then initialize its contents
async function getMonitoredResourceDescriptor(projectId, resourceType) {
// [START monitoring_get_resource]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.MetricServiceClient();
/**
* TODO(developer): Uncomment and edit the following lines of code.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const resourceType = 'some_resource_type, e.g. cloudsql_database';
const request = {
name: client.monitoredResourceDescriptorPath(projectId, resourceType),
};
// Lists monitored resource descriptors
const [descriptor] = await client.getMonitoredResourceDescriptor(request);
console.log(`Name: ${descriptor.displayName}`);
console.log(`Description: ${descriptor.description}`);
async function listMetricDescriptors(projectId) {
// [START monitoring_list_descriptors]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.MetricServiceClient();
/**
* TODO(developer): Uncomment and edit the following lines of code.
*/
// const projectId = 'YOUR_PROJECT_ID';
const request = {
name: client.projectPath(projectId),
};
// Lists metric descriptors
const [descriptors] = await client.listMetricDescriptors(request);
console.log('Metric Descriptors:');
descriptors.forEach(descriptor => console.log(descriptor.name));
// [END monitoring_list_descriptors]
async function deleteMetricDescriptor(projectId, metricId) {
// [START monitoring_delete_metric]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.MetricServiceClient();
/**
* TODO(developer): Uncomment and edit the following lines of code.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const metricId = 'custom.googleapis.com/stores/daily_sales';
const request = {
name: client.metricDescriptorPath(projectId, metricId),
};
// Deletes a metric descriptor
const [result] = await client.deleteMetricDescriptor(request);
console.log(`Deleted ${metricId}`, result);
// [END monitoring_delete_metric]
async function listMonitoredResourceDescriptors(projectId) {
// [START monitoring_list_resources]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.MetricServiceClient();
/**
* TODO(developer): Uncomment and edit the following lines of code.
*/
// const projectId = 'YOUR_PROJECT_ID';
const request = {
name: client.projectPath(projectId),
};
// Lists monitored resource descriptors
const [descriptors] = await client.listMonitoredResourceDescriptors(request);
console.log('Monitored Resource Descriptors:');
descriptors.forEach(descriptor => {
console.log(descriptor.name);
console.log(` Type: ${descriptor.type}`);
async function deleteUptimeCheckConfig(projectId, uptimeCheckConfigId) {
// [START monitoring_uptime_check_delete]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.UptimeCheckServiceClient();
/**
* TODO(developer): Uncomment and edit the following lines of code.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const uptimeCheckConfigId = 'YOUR_UPTIME_CHECK_CONFIG_ID';
const request = {
// i.e. name: 'projects/my-project-id/uptimeCheckConfigs/My-Uptime-Check
name: client.uptimeCheckConfigPath(projectId, uptimeCheckConfigId),
};
console.log(`Deleting ${request.name}`);
// Delete an uptime check config
await client.deleteUptimeCheckConfig(request);
async function updateUptimeCheckConfigDisplayName(
projectId,
uptimeCheckConfigId,
displayName,
path
) {
// [START monitoring_uptime_check_update]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.UptimeCheckServiceClient();
/**
* TODO(developer): Uncomment and edit the following lines of code.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const uptimeCheckConfigId = 'YOUR_UPTIME_CHECK_CONFIG_ID';
// const displayName = 'A New Display Name';
// const path = '/some/path';
const request = {
// i.e. name: 'projects/my-project-id/uptimeCheckConfigs/My-Uptime-Check
name: client.uptimeCheckConfigPath(projectId, uptimeCheckConfigId),
};
console.log(`Updating ${request.name} to ${displayName}`);
async function listUptimeCheckIps() {
// [START monitoring_uptime_check_list_ips]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.UptimeCheckServiceClient();
// List uptime check IPs
const [uptimeCheckIps] = await client.listUptimeCheckIps();
uptimeCheckIps.forEach(uptimeCheckIp => {
console.log(
uptimeCheckIp.region,
uptimeCheckIp.location,
uptimeCheckIp.ipAddress
);
});
// [END monitoring_uptime_check_list_ips]
}
async function getUptimeCheckConfig(projectId, uptimeCheckConfigId) {
// [START monitoring_uptime_check_get]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.UptimeCheckServiceClient();
/**
* TODO(developer): Uncomment and edit the following lines of code.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const uptimeCheckConfigId = 'YOUR_UPTIME_CHECK_CONFIG_ID';
const request = {
// i.e. name: 'projects/my-project-id/uptimeCheckConfigs/My-Uptime-Check
name: client.uptimeCheckConfigPath(projectId, uptimeCheckConfigId),
};
console.log(`Retrieving ${request.name}`);
// Retrieves an uptime check config
const [uptimeCheckConfig] = await client.getUptimeCheckConfig(request);
async function listUptimeCheckConfigs(projectId) {
// [START monitoring_uptime_check_list_configs]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.UptimeCheckServiceClient();
/**
* TODO(developer): Uncomment and edit the following lines of code.
*/
// const projectId = 'YOUR_PROJECT_ID';
const request = {
parent: client.projectPath(projectId),
};
// Retrieves an uptime check config
const [uptimeCheckConfigs] = await client.listUptimeCheckConfigs(request);
uptimeCheckConfigs.forEach(uptimeCheckConfig => {
console.log(`ID: ${uptimeCheckConfig.name}`);
console.log(` Display Name: ${uptimeCheckConfig.displayName}`);