<commit_before>/**
 * Created by <NAME> on 04/01/2017.
 */

'use strict';

const log = require("../../net2/logger.js")(__filename, "info");

const fs = require('fs');
const util = require('util');

const f = require('../../net2/Firewalla.js');
const fHome = f.getFirewallaHome();
const logFolder = f.getLogFolder();

const config = require("../../net2/config.js").getConfig();

const userID = f.getUserID();

const df = require('node-df');

const os  = require('../../vendor_lib/osutils.js');

const exec = require('child-process-promise').exec;

const rclient = require('../../util/redis_manager.js').getRedisClient()

const platformLoader = require('../../platform/PlatformLoader.js');
const platform = platformLoader.getPlatform();

let cpuUsage = 0;
let memUsage = 0;
let realMemUsage = 0;
let usedMem = 0;
let allMem = 0;
let curTemp = 0;
let peakTemp = 0;

let conn = 0;
let peakConn = 0;

let redisMemory = 0;

let updateFlag = 0;

let updateInterval = 60 * 1000; // every 30 seconds

let releaseBranch = null;

let threadInfo = {};

let diskInfo = null;

let intelQueueSize = 0;

let multiProfileSupport = false;

getMultiProfileSupportFlag();

async function update() {
  os.cpuUsage((v) => {
    log.debug( 'CPU Usage (%): ' + v );
    cpuUsage = v;
  });

  await getRealMemoryUsage();
  getTemp();
  await getConns();
  await getRedisMemoryUsage();
  await getThreadInfo();
  await getIntelQueueSize();
  await getDiskInfo();
  getMultiProfileSupportFlag();

  if(updateFlag) {
    setTimeout(() => { update(); }, updateInterval);
  }
}

function startUpdating() {
  updateFlag = 1;
  update();
}

function stopUpdating() {
  updateFlag = 0;
}

async function getThreadInfo() {
  try {
    const count = await exec("ps -Haux | wc -l", {encoding: 'utf8'});
    const mainCount = await exec("ps -Haux | grep Fi[r]eMain | wc -l", {encoding: 'utf8'});
    const apiCount = await exec("ps -Haux | grep Fi[r]eApi | wc -l", {encoding: 'utf8'});
    const monitorCount = await exec("ps -Haux | grep Fi[r]eMon | wc -l", {encoding: 'utf8'});
    threadInfo.count = count.stdout.replace("\n", "");
    threadInfo.mainCount = mainCount.stdout.replace("\n", "");
    threadInfo.apiCount = apiCount.stdout.replace("\n", "");
    threadInfo.monitorCount = monitorCount.stdout.replace("\n", "");
  } catch(err) {
    log.error("Failed to get thread info", err);
  }
}

function getDiskInfo() {
  return new Promise((resolve, reject) => {
    df((err, response) => {
      if(err) {
        log.error("Failed to get disk info", err);
        resolve();
        return
      }

      const disks = response.filter((entry) => {
        return entry.filesystem.startsWith("/dev/mmc");
      })

      diskInfo = disks;

      resolve();
    });
  })
}

async function getMultiProfileSupportFlag() {
  const cmd = "sudo bash -c 'test ! -e /etc/openvpn/easy-rsa/keys2 && test -e /etc/openvpn/multi_profile_support'"
  try {
    await exec(cmd);
    multiProfileSupport = true;
  } catch(err) {
    multiProfileSupport = false;
  }
}

async function getIntelQueueSize() {
  intelQueueSize = await rclient.zcountAsync("ip_set_to_be_processed", "-inf", "+inf");
}

async function getRealMemoryUsage() {
  try {
    const res = await exec('free');<commit_msg>refine check condition<commit_after>/**
 * Created by <NAME> on 04/01/2017.
 */

'use strict';

const log = require("../../net2/logger.js")(__filename, "info");

const fs = require('fs');
const util = require('util');

const f = require('../../net2/Firewalla.js');
const fHome = f.getFirewallaHome();
const logFolder = f.getLogFolder();

const config = require("../../net2/config.js").getConfig();

const userID = f.getUserID();

const df = require('node-df');

const os  = require('../../vendor_lib/osutils.js');

const exec = require('child-process-promise').exec;

const rclient = require('../../util/redis_manager.js').getRedisClient()

const platformLoader = require('../../platform/PlatformLoader.js');
const platform = platformLoader.getPlatform();

let cpuUsage = 0;
let memUsage = 0;
let realMemUsage = 0;
let usedMem = 0;
let allMem = 0;
let curTemp = 0;
let peakTemp = 0;

let conn = 0;
let peakConn = 0;

let redisMemory = 0;

let updateFlag = 0;

let updateInterval = 60 * 1000; // every 30 seconds

let releaseBranch = null;

let threadInfo = {};

let diskInfo = null;

let intelQueueSize = 0;

let multiProfileSupport = false;

getMultiProfileSupportFlag();

async function update() {
  os.cpuUsage((v) => {
    log.debug( 'CPU Usage (%): ' + v );
    cpuUsage = v;
  });

  await getRealMemoryUsage();
  getTemp();
  await getConns();
  await getRedisMemoryUsage();
  await getThreadInfo();
  await getIntelQueueSize();
  await getDiskInfo();
  getMultiProfileSupportFlag();

  if(updateFlag) {
    setTimeout(() => { update(); }, updateInterval);
  }
}

function startUpdating() {
  updateFlag = 1;
  update();
}

function stopUpdating() {
  updateFlag = 0;
}

async function getThreadInfo() {
  try {
    const count = await exec("ps -Haux | wc -l", {encoding: 'utf8'});
    const mainCount = await exec("ps -Haux | grep Fi[r]eMain | wc -l", {encoding: 'utf8'});
    const apiCount = await exec("ps -Haux | grep Fi[r]eApi | wc -l", {encoding: 'utf8'});
    const monitorCount = await exec("ps -Haux | grep Fi[r]eMon | wc -l", {encoding: 'utf8'});
    threadInfo.count = count.stdout.replace("\n", "");
    threadInfo.mainCount = mainCount.stdout.replace("\n", "");
    threadInfo.apiCount = apiCount.stdout.replace("\n", "");
    threadInfo.monitorCount = monitorCount.stdout.replace("\n", "");
  } catch(err) {
    log.error("Failed to get thread info", err);
  }
}

function getDiskInfo() {
  return new Promise((resolve, reject) => {
    df((err, response) => {
      if(err) {
        log.error("Failed to get disk info", err);
        resolve();
        return
      }

      const disks = response.filter((entry) => {
        return entry.filesystem.startsWith("/dev/mmc");
      })

      diskInfo = disks;

      resolve();
    });
  })
}

async function getMultiProfileSupportFlag() {
  const cmd = "sudo bash -c 'test -e /etc/openvpn/easy-rsa/keys2/ta.key'"
  try {
    await exec(cmd);
    multiProfileSupport = false;
  } catch(err) {
    multiProfileSupport = true;
  }
}

async function getIntelQueueSize() {
  intelQueueSize = await rclient.zcountAsync("ip_set_to_be_processed", "-inf", "+inf");
}

async function getRealMemoryUsage() {
  try {
    const res = await exec('free');
{'fraction_non_alphanumeric': 0.16893775162589036, 'fraction_numerical': 0.011768349334159182, 'mean_word_length': 3.491655076495132, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6859695', 'n_tokens_mistral': 2199, 'n_tokens_neox': 2142, 'n_words': 532}