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
|
|
|
|
|
2010-02-25 09:54:18 -05:00
|
|
|
float cpu_percentage( unsigned int cpu_usage_delay )
|
2009-09-09 02:28:04 -04:00
|
|
|
{
|
2013-04-24 22:13:50 -04:00
|
|
|
std::string stat_line;
|
2010-02-24 19:49:10 -05:00
|
|
|
size_t line_start_pos;
|
|
|
|
size_t line_end_pos;
|
|
|
|
unsigned long long current_user;
|
|
|
|
unsigned long long current_system;
|
|
|
|
unsigned long long current_nice;
|
|
|
|
unsigned long long current_idle;
|
|
|
|
unsigned long long next_user;
|
|
|
|
unsigned long long next_system;
|
|
|
|
unsigned long long next_nice;
|
|
|
|
unsigned long long next_idle;
|
|
|
|
unsigned long long diff_user;
|
|
|
|
unsigned long long diff_system;
|
|
|
|
unsigned long long diff_nice;
|
|
|
|
unsigned long long diff_idle;
|
2013-04-24 22:13:50 -04:00
|
|
|
std::istringstream iss;
|
2010-02-24 19:49:10 -05:00
|
|
|
|
2013-04-24 22:13:50 -04:00
|
|
|
std::ifstream stat_file("/proc/stat");
|
2010-02-24 19:49:10 -05:00
|
|
|
getline(stat_file, stat_line);
|
|
|
|
stat_file.close();
|
2009-09-09 02:28:04 -04:00
|
|
|
|
2010-02-24 19:49:10 -05:00
|
|
|
// skip "cpu"
|
|
|
|
line_start_pos = stat_line.find_first_not_of(" ", 3);
|
|
|
|
line_end_pos = stat_line.find_first_of(' ', line_start_pos);
|
|
|
|
line_end_pos = stat_line.find_first_of(' ', line_end_pos + 1);
|
|
|
|
line_end_pos = stat_line.find_first_of(' ', line_end_pos + 1);
|
|
|
|
line_end_pos = stat_line.find_first_of(' ', line_end_pos + 1);
|
2009-09-09 02:28:04 -04:00
|
|
|
|
2010-02-24 19:49:10 -05:00
|
|
|
iss.str( stat_line.substr( line_start_pos, line_end_pos - line_start_pos ) );
|
|
|
|
iss >> current_user >> current_nice >> current_system >> current_idle;
|
|
|
|
iss.clear();
|
|
|
|
|
2010-02-25 09:54:18 -05:00
|
|
|
usleep( cpu_usage_delay );
|
2010-02-24 19:49:10 -05:00
|
|
|
|
|
|
|
stat_file.open("/proc/stat");
|
|
|
|
getline(stat_file, stat_line);
|
|
|
|
stat_file.close();
|
2009-09-09 02:28:04 -04:00
|
|
|
|
|
|
|
// skip "cpu"
|
2010-02-24 19:49:10 -05:00
|
|
|
line_start_pos = stat_line.find_first_not_of(" ", 3);
|
|
|
|
line_end_pos = stat_line.find_first_of(' ', line_start_pos);
|
|
|
|
line_end_pos = stat_line.find_first_of(' ', line_end_pos + 1);
|
|
|
|
line_end_pos = stat_line.find_first_of(' ', line_end_pos + 1);
|
|
|
|
line_end_pos = stat_line.find_first_of(' ', line_end_pos + 1);
|
|
|
|
|
|
|
|
iss.str( stat_line.substr( line_start_pos, line_end_pos - line_start_pos ) );
|
|
|
|
iss >> next_user >> next_nice >> next_system >> next_idle;
|
|
|
|
iss.clear();
|
|
|
|
|
|
|
|
diff_user = next_user - current_user;
|
|
|
|
diff_system = next_system - current_system;
|
|
|
|
diff_nice = next_nice - current_nice;
|
|
|
|
diff_idle = next_idle - current_idle;
|
|
|
|
|
|
|
|
return static_cast<float>(diff_user + diff_system + diff_nice)/static_cast<float>(diff_user + diff_system + diff_nice + diff_idle)*100.0;
|
2009-09-09 02:28:04 -04:00
|
|
|
}
|
|
|
|
|
2013-04-24 22:13:50 -04:00
|
|
|
std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines )
|
2009-09-09 02:28:04 -04:00
|
|
|
{
|
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;
|
2010-08-01 11:17:42 -04:00
|
|
|
while(meter_count*meter_step < percentage)
|
2009-09-09 02:28:04 -04:00
|
|
|
{
|
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
|
|
|
|
|
|
|
oss << meter;
|
|
|
|
oss.width( 5 );
|
|
|
|
oss << percentage;
|
|
|
|
oss << "%";
|
|
|
|
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2013-04-24 22:13:50 -04:00
|
|
|
std::string mem_string()
|
2010-02-24 20:02:55 -05:00
|
|
|
{
|
2010-02-25 00:37:21 -05:00
|
|
|
unsigned int total_mem;
|
|
|
|
unsigned int used_mem;
|
|
|
|
unsigned int unused_mem;
|
2010-02-24 20:02:55 -05:00
|
|
|
size_t line_start_pos;
|
|
|
|
size_t line_end_pos;
|
2013-04-24 22:13:50 -04:00
|
|
|
std::istringstream iss;
|
|
|
|
std::ostringstream oss;
|
|
|
|
std::string mem_line;
|
2010-02-24 20:02:55 -05:00
|
|
|
|
2013-04-24 22:13:50 -04:00
|
|
|
std::ifstream meminfo_file( "/proc/meminfo" );
|
2010-02-24 20:02:55 -05:00
|
|
|
getline( meminfo_file, mem_line );
|
|
|
|
line_start_pos = mem_line.find_first_of( ':' );
|
|
|
|
line_start_pos++;
|
|
|
|
line_end_pos = mem_line.find_first_of( 'k' );
|
|
|
|
iss.str( mem_line.substr( line_start_pos, line_end_pos - line_start_pos ) );
|
|
|
|
iss >> total_mem;
|
|
|
|
|
2010-02-25 00:37:21 -05:00
|
|
|
used_mem = total_mem;
|
2010-02-24 20:02:55 -05:00
|
|
|
|
2010-02-25 00:37:21 -05:00
|
|
|
for( unsigned int i = 0; i < 3; i++ )
|
|
|
|
{
|
|
|
|
getline( meminfo_file, mem_line );
|
|
|
|
line_start_pos = mem_line.find_first_of( ':' );
|
|
|
|
line_start_pos++;
|
|
|
|
line_end_pos = mem_line.find_first_of( 'k' );
|
|
|
|
iss.str( mem_line.substr( line_start_pos, line_end_pos - line_start_pos ) );
|
|
|
|
iss >> unused_mem;
|
|
|
|
used_mem -= unused_mem;
|
|
|
|
}
|
2010-02-24 20:02:55 -05:00
|
|
|
meminfo_file.close();
|
|
|
|
|
2010-02-25 01:03:02 -05:00
|
|
|
oss << used_mem / 1024 << '/' << total_mem / 1024 << "MB";
|
2010-02-25 00:37:21 -05:00
|
|
|
|
2010-02-24 20:02:55 -05:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2013-04-24 22:13:50 -04:00
|
|
|
std::string load_string()
|
2010-02-25 00:49:36 -05:00
|
|
|
{
|
2013-04-24 22:13:50 -04:00
|
|
|
std::ifstream loadavg_file( "/proc/loadavg" );
|
|
|
|
std::string load_line;
|
|
|
|
std::getline( loadavg_file, load_line );
|
2010-02-25 00:49:36 -05:00
|
|
|
loadavg_file.close();
|
|
|
|
|
|
|
|
return load_line.substr( 0, 14 );
|
|
|
|
}
|
|
|
|
|
2010-02-24 19:49:10 -05:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2010-02-25 09:54:18 -05:00
|
|
|
unsigned int cpu_usage_delay = 900000;
|
2010-08-01 11:17:42 -04:00
|
|
|
unsigned int graph_lines = 10;
|
2013-02-21 00:08:14 -05:00
|
|
|
bool use_colors = false;
|
2010-08-01 11:17:42 -04:00
|
|
|
try
|
2010-02-25 09:54:18 -05:00
|
|
|
{
|
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] );
|
2010-02-25 09:54:18 -05:00
|
|
|
unsigned int status_interval;
|
|
|
|
iss >> status_interval;
|
|
|
|
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;
|
|
|
|
}
|
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;
|
2010-08-01 11:17:42 -04:00
|
|
|
return 1;
|
2013-02-20 23:15:20 -05:00
|
|
|
}
|
2010-08-01 11:17:42 -04:00
|
|
|
|
2013-02-21 00:08:14 -05:00
|
|
|
std::cout << use_colors << " " << cpu_usage_delay << " " << graph_lines << std::endl;
|
2010-08-01 11:17:42 -04:00
|
|
|
std::cout << mem_string() << ' ' << cpu_string( cpu_usage_delay, graph_lines ) << ' ' << load_string();
|
2010-02-25 09:54:18 -05:00
|
|
|
|
2009-09-09 02:28:04 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|