Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

battery check when writing to log #496

Merged
merged 3 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/agent/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,4 @@ exports.remove_files = function(cb) {
if (entry.file && entry.content_type)
fs.unlink(entry.file, done);
});
}
}
28 changes: 19 additions & 9 deletions lib/agent/providers/indicators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@
var
os = require('os'),
os_name = process.platform.replace('darwin', 'mac').replace('win32', 'windows'),
os_functions = require('./' + os_name);
os_functions = require('./' + os_name),
battery_available = true;

/**
* Callsback current uptime in seconds.
**/
exports.get_uptime = function(callback){
exports.get_uptime = function (callback) {
callback(null, parseInt(os.uptime()));
};

/**
* Callsback percentage of battery remaining in integer: ('80%' -> 80)
**/
exports.get_remaining_battery = function(callback){
os_functions.get_battery_status(function(err, data){
exports.get_remaining_battery = function (callback) {
os_functions.get_battery_status(function (err, data) {
if (err || !data)
return callback(err || new Error("Couldn't read battery status."));

Expand All @@ -34,7 +35,7 @@ exports.get_remaining_battery = function(callback){
/**
* Callsback object with last_min, last_five and last_fifteen fields of load
**/
exports.get_cpu_load = function(callback){
exports.get_cpu_load = function (callback) {
var data = os.loadavg();

var info = {
Expand All @@ -46,15 +47,15 @@ exports.get_cpu_load = function(callback){
callback(null, info);
};

exports.get_memory_usage = function(callback) {
var trimInt = function(number, len){
exports.get_memory_usage = function (callback) {
var trimInt = function (number, len) {
return number.toString().substring(0, len || 4);
};

var mem_usage = {
total_bytes: os.totalmem(),
free_bytes: os.freemem(),
used: 100 - trimInt(os.freemem()*100/os.totalmem()) + '%'
used: 100 - trimInt(os.freemem() * 100 / os.totalmem()) + '%'
};
// mem_usage.used = 100 - parseFloat(mem_usage.remaining) + '%';

Expand All @@ -64,7 +65,16 @@ exports.get_memory_usage = function(callback) {
/**
* Get battery info.
**/
exports.get_battery_status = os_functions.get_battery_status;
exports.get_battery_status = function (callback) {
if (!battery_available) return callback();
os_functions.get_battery_status((err, stdout) => {
if (err && err.message == 'No Instance(s) Available.') {
battery_available = false;
callback()
}
callback(err, stdout)
});
}

/**
* Callsback an object with fields size_gb, free_gb, used
Expand Down