2012-11-25 12:19:50 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Matthew McCormick
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
* */
|
2010-02-24 19:49:10 -05:00
|
|
|
|
2013-02-21 00:08:14 -05:00
|
|
|
#include <cstring>
|
2009-09-09 02:28:04 -04:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <unistd.h> // sleep
|
2013-06-09 20:49:21 -04:00
|
|
|
#include <cmath> // for floorf
|
2013-06-09 21:01:32 -04:00
|
|
|
#include <cstdlib> // EXIT_SUCCESS
|
2009-09-09 02:28:04 -04:00
|
|
|
|
2014-04-14 14:51:46 -04:00
|
|
|
// Tmux color lookup tables for the different metrics.
|
|
|
|
#include "luts.h"
|
|
|
|
|
2013-04-15 15:15:31 -04:00
|
|
|
#if defined(__APPLE__) && defined(__MACH__)
|
2014-04-14 14:51:46 -04:00
|
|
|
// Apple osx system
|
|
|
|
#include "osx/cpu.h"
|
|
|
|
#include "osx/memory.h"
|
|
|
|
#include "osx/load.h"
|
|
|
|
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
|
|
|
|
// BSD system
|
|
|
|
// TODO: Includes and *BSD support
|
|
|
|
#define BSD_BASED 1
|
|
|
|
// include _get_cpu_percentage (see osx/cpu.cc)
|
|
|
|
// include cpu_percentage (see osx/cpu.cc)
|
|
|
|
#else
|
|
|
|
// assume linux system
|
|
|
|
#include "linux/cpu.h"
|
|
|
|
#include "linux/memory.h"
|
|
|
|
#include "linux/load.h"
|
2013-04-15 15:15:31 -04:00
|
|
|
#endif
|
2009-09-09 02:28:04 -04:00
|
|
|
|
2013-04-30 06:38:39 -04:00
|
|
|
|
2013-06-09 20:49:21 -04:00
|
|
|
// Function declarations.
|
2014-04-14 14:51:46 -04:00
|
|
|
// TODO: those should stay in separate headers
|
|
|
|
// LINUX: DONE/partial
|
|
|
|
// OSX: DONE/partial
|
|
|
|
// BSD: TODO
|
2013-04-15 15:15:31 -04:00
|
|
|
float cpu_percentage( unsigned int cpu_usage_delay );
|
2009-09-09 02:28:04 -04:00
|
|
|
|
2013-04-30 06:38:39 -04:00
|
|
|
std::string cpu_string( unsigned int cpu_usage_delay,
|
2014-04-14 14:51:46 -04:00
|
|
|
unsigned int graph_lines, bool use_colors = false ) {
|
2013-04-24 22:13:50 -04:00
|
|
|
std::string meter( graph_lines + 2, ' ' );
|
2010-08-01 11:17:42 -04:00
|
|
|
meter[0] = '[';
|
|
|
|
meter[meter.length() - 1] = ']';
|
2009-09-09 02:28:04 -04:00
|
|
|
int meter_count = 0;
|
2010-02-24 19:49:10 -05:00
|
|
|
float percentage;
|
2013-04-24 22:13:50 -04:00
|
|
|
std::ostringstream oss;
|
2010-02-24 19:49:10 -05:00
|
|
|
oss.precision( 1 );
|
2013-04-24 22:13:50 -04:00
|
|
|
oss.setf( std::ios::fixed | std::ios::right );
|
2010-02-24 19:49:10 -05:00
|
|
|
|
2010-02-25 09:54:18 -05:00
|
|
|
percentage = cpu_percentage( cpu_usage_delay );
|
2010-08-01 11:17:42 -04:00
|
|
|
float meter_step = 99.9 / graph_lines;
|
2010-02-24 19:49:10 -05:00
|
|
|
meter_count = 1;
|
2014-04-14 14:51:46 -04:00
|
|
|
|
|
|
|
while(meter_count*meter_step < percentage) {
|
2010-02-24 19:49:10 -05:00
|
|
|
meter[meter_count] = '|';
|
|
|
|
meter_count++;
|
2009-09-09 02:28:04 -04:00
|
|
|
}
|
2010-02-24 19:49:10 -05:00
|
|
|
|
2013-04-30 06:38:39 -04:00
|
|
|
if( use_colors )
|
|
|
|
oss << cpu_percentage_lut[static_cast<unsigned int>( percentage )];
|
2014-04-14 14:51:46 -04:00
|
|
|
|
2010-02-24 19:49:10 -05:00
|
|
|
oss << meter;
|
|
|
|
oss.width( 5 );
|
|
|
|
oss << percentage;
|
|
|
|
oss << "%";
|
2013-04-30 06:38:39 -04:00
|
|
|
if( use_colors )
|
|
|
|
oss << "#[fg=default,bg=default]";
|
2010-02-25 00:37:21 -05:00
|
|
|
|
2010-02-24 20:02:55 -05:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2014-04-14 14:51:46 -04:00
|
|
|
int main(int argc, char** argv) {
|
2010-02-25 09:54:18 -05:00
|
|
|
unsigned int cpu_usage_delay = 900000;
|
2013-06-09 20:58:47 -04:00
|
|
|
int graph_lines = 10;
|
2013-02-21 00:08:14 -05:00
|
|
|
bool use_colors = false;
|
2014-04-14 14:51:46 -04:00
|
|
|
try {
|
2013-04-24 22:13:50 -04:00
|
|
|
std::istringstream iss;
|
|
|
|
iss.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
|
2013-02-21 00:08:14 -05:00
|
|
|
std::string current_arg;
|
|
|
|
unsigned int arg_index = 1;
|
|
|
|
if( argc > arg_index )
|
2013-02-20 23:15:20 -05:00
|
|
|
{
|
2013-02-21 00:08:14 -05:00
|
|
|
if( strcmp( argv[arg_index], "--colors" ) == 0 )
|
|
|
|
{
|
|
|
|
use_colors = true;
|
|
|
|
++arg_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( argc > arg_index )
|
|
|
|
{
|
|
|
|
iss.str( argv[arg_index] );
|
2013-06-09 22:24:28 -04:00
|
|
|
int status_interval;
|
2010-02-25 09:54:18 -05:00
|
|
|
iss >> status_interval;
|
2013-06-09 22:24:28 -04:00
|
|
|
if( status_interval < 1 )
|
|
|
|
{
|
|
|
|
std::cerr << "Status interval argument must be one or greater." << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2010-02-25 09:54:18 -05:00
|
|
|
cpu_usage_delay = status_interval * 1000000 - 100000;
|
2013-02-21 00:08:14 -05:00
|
|
|
++arg_index;
|
2013-02-20 23:15:20 -05:00
|
|
|
}
|
2013-02-21 00:08:14 -05:00
|
|
|
if( argc > arg_index )
|
2013-02-20 23:15:20 -05:00
|
|
|
{
|
2013-02-21 00:08:14 -05:00
|
|
|
iss.str( argv[arg_index] );
|
2013-02-20 23:15:20 -05:00
|
|
|
iss.clear();
|
|
|
|
iss >> graph_lines;
|
2013-06-09 20:58:47 -04:00
|
|
|
if( graph_lines < 1 )
|
|
|
|
{
|
2013-06-09 22:24:28 -04:00
|
|
|
std::cerr << "Graph lines argument must be one or greater." << std::endl;
|
2013-06-09 21:01:32 -04:00
|
|
|
return EXIT_FAILURE;
|
2013-06-09 20:58:47 -04:00
|
|
|
}
|
2013-02-20 23:15:20 -05:00
|
|
|
}
|
2010-02-25 09:54:18 -05:00
|
|
|
}
|
2013-04-24 22:13:50 -04:00
|
|
|
catch(const std::exception &e)
|
2013-02-20 23:15:20 -05:00
|
|
|
{
|
2013-04-24 22:13:50 -04:00
|
|
|
std::cerr << "Usage: " << argv[0] << " [--colors] [tmux_status-interval(seconds)] [graph lines]" << std::endl;
|
2013-06-09 21:01:32 -04:00
|
|
|
return EXIT_FAILURE;
|
2013-02-20 23:15:20 -05:00
|
|
|
}
|
2010-08-01 11:17:42 -04:00
|
|
|
|
2013-04-30 06:38:39 -04:00
|
|
|
std::cout << mem_string( use_colors ) << ' ' << cpu_string( cpu_usage_delay, graph_lines, use_colors ) << ' ' << load_string( use_colors );
|
2010-02-25 09:54:18 -05:00
|
|
|
|
2013-06-09 21:01:32 -04:00
|
|
|
return EXIT_SUCCESS;
|
2009-09-09 02:28:04 -04:00
|
|
|
}
|
|
|
|
|