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 quickstart() {
// Your Google Cloud Platform project ID
const projectId = process.env.GCLOUD_PROJECT || 'YOUR_PROJECT_ID';
// Creates a client
const client = new monitoring.MetricServiceClient();
// Prepares an individual data point
const dataPoint = {
interval: {
endTime: {
seconds: Date.now() / 1000,
},
},
value: {
// The amount of sales
doubleValue: 123.45,
},
};
// Prepares the time series request
const request = {
async function createMetricDescriptor(projectId) {
// [START monitoring_create_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 request = {
name: client.projectPath(projectId),
metricDescriptor: {
description: 'Daily sales records from all branch stores.',
displayName: 'Daily Sales',
type: 'custom.googleapis.com/stores/daily_sales',
metricKind: 'GAUGE',
valueType: 'DOUBLE',
unit: '{USD}',
labels: [
try {
await metricServiceClient.createTimeSeries(writeRequest);
node.send(msg);
} catch(err) {
node.error(err);
}
} // Input
// We must have EITHER credentials or a keyFilename. If neither are supplied, that
// is an error. If both are supplied, then credentials will be used.
if (credentials) {
metricServiceClient = new monitoring.MetricServiceClient({
"credentials": credentials
});
} else if (keyFilename) {
metricServiceClient = new monitoring.MetricServiceClient({
"keyFilename": keyFilename
});
} else {
metricServiceClient = new monitoring.MetricServiceClient({});
}
node.on('input', Input);
} // MonitoringNode