1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/*
* Phusion Passenger - https://www.phusionpassenger.com/
* Copyright (c) 2014 Phusion
*
* "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <iostream>
#include <string>
#include <unistd.h>
#include <cstdio>
#include <cstring>
#include <Utils.h>
#include <Utils/StrIntUtils.h>
#include <Utils/SystemMetricsCollector.h>
namespace SystemMetricsTool {
using namespace std;
using namespace Passenger;
struct Options {
bool xml;
SystemMetrics::XmlOptions xmlOptions;
SystemMetrics::DescriptionOptions descOptions;
int interval;
bool useStdin;
bool exitOnUnexpectedError;
bool help;
Options() {
xml = false;
descOptions.colors = isatty(1);
interval = -1;
useStdin = false;
exitOnUnexpectedError = true;
help = false;
}
};
static bool
isFlag(const char *arg, char shortFlagName, const char *longFlagName) {
return strcmp(arg, longFlagName) == 0
|| (shortFlagName != '\0' && arg[0] == '-'
&& arg[1] == shortFlagName && arg[2] == '\0');
}
static void
usage() {
printf("Usage: passenger-config system-metrics [OPTIONS]\n");
printf("Displays various metrics about the system.\n");
printf("\n");
printf("Options:\n");
printf(" --xml Output in XML format\n");
printf(" --no-general Do not display general metrics\n");
printf(" --no-cpu Do not display CPU metrics\n");
printf(" --no-memory Do not display memory metrics\n");
printf(" --force-colors Display colors even if stdout is not a TTY\n");
printf(" -w --watch INTERVAL Reprint metrics every INTERVAL seconds\n");
printf(" --stdin Reprint metrics every time a newline is received on\n");
printf(" stdin, until EOF. Mutually exclusive with --watch\n");
printf(" --no-exit-on-unexpected-error Normally, if an unexpected error is\n");
printf(" encountered while collecting system metrics, this\n");
printf(" program will exit with an error code. This option\n");
printf(" suppresses that\n");
printf(" -h, --help Show this help\n");
}
static Options
parseOptions(int argc, char *argv[]) {
Options options;
int i = 2;
while (i < argc) {
if (isFlag(argv[i], '\0', "--xml")) {
options.xml = true;
i++;
} else if (isFlag(argv[i], '\0', "--no-general")) {
options.xmlOptions.general = false;
options.descOptions.general = false;
i++;
} else if (isFlag(argv[i], '\0', "--no-cpu")) {
options.xmlOptions.cpu = false;
options.descOptions.cpu = false;
i++;
} else if (isFlag(argv[i], '\0', "--no-memory")) {
options.xmlOptions.memory = false;
options.descOptions.memory = false;
i++;
} else if (isFlag(argv[i], '\0', "--force-colors")) {
options.descOptions.colors = true;
i++;
} else if (isFlag(argv[i], 'w', "--watch")) {
if (argc >= i + 2) {
options.interval = atoi(argv[i + 1]);
i += 2;
} else {
fprintf(stderr, "ERROR: extra argument required for --watch\n");
usage();
exit(1);
}
} else if (isFlag(argv[i], '\0', "--stdin")) {
options.useStdin = true;
i++;
} else if (isFlag(argv[i], '\0', "--no-exit-on-unexpected-error")) {
options.exitOnUnexpectedError = false;
i++;
} else if (isFlag(argv[i], 'h', "--help")) {
options.help = true;
i++;
} else {
fprintf(stderr, "ERROR: unrecognized argument %s\n", argv[i]);
usage();
exit(1);
}
}
if (options.interval != -1 && options.useStdin) {
fprintf(stderr, "ERROR: --watch and --stdin are mutually exclusive.\n");
exit(1);
}
return options;
}
static bool
waitForNextLine() {
char buf[1024];
return fgets(buf, sizeof(buf), stdin) != NULL;
}
static void
perform(const Options &options, SystemMetricsCollector &collector, SystemMetrics &metrics) {
try {
collector.collect(metrics);
if (options.xml) {
cout << "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
metrics.toXml(cout, options.xmlOptions);
cout << endl;
} else {
metrics.toDescription(cout, options.descOptions);
}
} catch (const RuntimeException &e) {
fprintf(stderr, "An error occurred while collecting system metrics: %s\n", e.what());
if (options.exitOnUnexpectedError) {
exit(1);
}
}
}
static int
main(int argc, char *argv[]) {
Options options = parseOptions(argc, argv);
if (options.help) {
usage();
return 0;
}
SystemMetricsCollector collector;
SystemMetrics metrics;
if (options.descOptions.cpu) {
collector.collect(metrics);
// We have to measure system metrics within an interval
// in order to determine the CPU usage.
usleep(50000);
}
if (options.useStdin) {
while (waitForNextLine()) {
perform(options, collector, metrics);
}
} else {
do {
perform(options, collector, metrics);
if (options.interval != -1) {
sleep(options.interval);
}
} while (options.interval != -1);
}
return 0;
}
} // namespace SystemMetricsTool
|