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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
/** @file
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
*/
#include "tscore/ink_config.h"
#include "tscore/ink_defs.h"
#include "ts/ts.h"
#include <dirent.h>
#include <getopt.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef HAVE_SYS_SYSINFO_H
#include <sys/sysinfo.h>
#endif
#include <limits.h>
#define PLUGIN_NAME "system_stats"
#define DEBUG_TAG PLUGIN_NAME
// Time in MS to grab the system stats
#define SYSTEM_STATS_TIMEOUT 5000
// Load Average Strings
#define LOAD_AVG_ONE_MIN "plugin." PLUGIN_NAME ".loadavg.one"
#define LOAD_AVG_FIVE_MIN "plugin." PLUGIN_NAME ".loadavg.five"
#define LOAD_AVG_FIFTEEN_MIN "plugin." PLUGIN_NAME ".loadavg.fifteen"
// Process Strings
#define CURRENT_PROCESSES "plugin." PLUGIN_NAME ".current_processes"
// Base net stats name, full name needs to populated
// with NET_STATS.infname.RX/TX.standard_net_stats field
#define NET_STATS "plugin." PLUGIN_NAME ".net."
#define NET_STATS_DIR "/sys/class/net"
#define STATISTICS_DIR "statistics"
// Used for matching to slave (old name) and lower (new name) symlinks
// in a bonded interface
// This way we can report things like plugin.net.bond0.slave_dev1.speed
#define SLAVE "slave_"
#define LOWER "lower_"
// Dir name for slave/lower interfaces that are bond members. This dir houses
// port information we may want such as the up/down streams port state
#define BONDING_SLAVE_DIR "bonding_slave"
static int
statAdd(const char *name, TSRecordDataType record_type, TSMutex create_mutex)
{
int stat_id = -1;
TSMutexLock(create_mutex);
if (TS_ERROR == TSStatFindName(name, &stat_id)) {
stat_id = TSStatCreate(name, record_type, TS_STAT_NON_PERSISTENT, TS_STAT_SYNC_SUM);
if (stat_id == TS_ERROR) {
TSDebug(DEBUG_TAG, "Error creating stat_name: %s", name);
} else {
TSDebug(DEBUG_TAG, "Created stat_name: %s stat_id: %d", name, stat_id);
}
}
TSMutexUnlock(create_mutex);
return stat_id;
}
static int
getFile(const char *filename, char *buffer, int bufferSize)
{
TSFile f = 0;
size_t s = 0;
f = TSfopen(filename, "r");
if (!f) {
buffer[0] = 0;
// Return -1 to indicate read err
return -1;
}
s = TSfread(f, buffer, bufferSize);
if (s > 0) {
buffer[s] = 0;
} else {
buffer[0] = 0;
}
TSfclose(f);
return s;
}
static void
statSet(const char *name, long value, TSMutex stat_creation_mutex)
{
int stat_id = statAdd(name, TS_RECORDDATATYPE_INT, stat_creation_mutex);
if (stat_id != TS_ERROR) {
TSStatIntSet(stat_id, value);
}
}
static void
setNetStat(TSMutex stat_creation_mutex, const char *interface, const char *entry, const char *subdir, bool subdirstatname)
{
char sysfs_name[PATH_MAX];
char stat_name[255];
char data[255];
memset(&stat_name[0], 0, sizeof(stat_name));
memset(&sysfs_name[0], 0, sizeof(sysfs_name));
memset(&data[0], 0, sizeof(data));
if ((interface == NULL) || (entry == NULL)) {
TSError("%s: NULL subdir or entry", DEBUG_TAG);
return;
}
// Generate the ATS stats name
if (subdirstatname) {
snprintf(&stat_name[0], sizeof(stat_name), "%s%s.%s.%s", NET_STATS, interface, subdir, entry);
} else {
snprintf(&stat_name[0], sizeof(stat_name), "%s%s.%s", NET_STATS, interface, entry);
}
// Determine if this is a toplevel netdev stat, or one from statistics.
if (subdir == NULL) {
snprintf(&sysfs_name[0], sizeof(sysfs_name), "%s/%s/%s", NET_STATS_DIR, interface, entry);
} else {
snprintf(&sysfs_name[0], sizeof(sysfs_name), "%s/%s/%s/%s", NET_STATS_DIR, interface, subdir, entry);
}
if (getFile(&sysfs_name[0], &data[0], sizeof(data)) < 0) {
TSDebug(DEBUG_TAG, "Error reading file %s", sysfs_name);
} else {
statSet(stat_name, atol(data), stat_creation_mutex);
}
}
static void
setBondingStat(TSMutex stat_creation_mutex, const char *interface)
{
char infdir[PATH_MAX];
struct dirent *dent;
memset(&infdir[0], 0, sizeof(infdir));
if (interface == NULL) {
TSError("%s: NULL interface", DEBUG_TAG);
return;
}
snprintf(&infdir[0], sizeof(infdir), "%s/%s", NET_STATS_DIR, interface);
DIR *localdir = opendir(infdir);
while ((dent = readdir(localdir)) != NULL) {
if (((strncmp(SLAVE, dent->d_name, strlen(SLAVE)) == 0) || (strncmp(LOWER, dent->d_name, strlen(LOWER)) == 0)) &&
(dent->d_type == DT_LNK)) {
// We have a symlink starting with slave or lower, get its speed
setNetStat(stat_creation_mutex, interface, "speed", dent->d_name, true);
}
if (strncmp(BONDING_SLAVE_DIR, dent->d_name, strlen(BONDING_SLAVE_DIR)) == 0 && (dent->d_type != DT_LNK)) {
setNetStat(stat_creation_mutex, interface, "ad_actor_oper_port_state", dent->d_name, false);
setNetStat(stat_creation_mutex, interface, "ad_partner_oper_port_state", dent->d_name, false);
}
}
closedir(localdir);
}
static int
netStatsInfo(TSMutex stat_creation_mutex)
{
struct dirent *dent;
DIR *srcdir = opendir(NET_STATS_DIR);
if (srcdir == NULL) {
return 0;
}
while ((dent = readdir(srcdir)) != NULL) {
if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0 || (dent->d_type != DT_LNK)) {
continue;
}
setNetStat(stat_creation_mutex, dent->d_name, "speed", NULL, false);
setNetStat(stat_creation_mutex, dent->d_name, "collisions", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "multicast", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "rx_bytes", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "rx_compressed", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "rx_crc_errors", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "rx_dropped", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "rx_errors", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "rx_fifo_errors", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "rx_frame_errors", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "rx_length_errors", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "rx_missed_errors", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "rx_nohandler", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "rx_over_errors", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "rx_packets", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "tx_aborted_errors", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "tx_bytes", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "tx_carrier_errors", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "tx_compressed", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "tx_dropped", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "tx_errors", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "tx_fifo_errors", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "tx_heartbeat_errors", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "tx_packets", STATISTICS_DIR, false);
setNetStat(stat_creation_mutex, dent->d_name, "tx_window_errors", STATISTICS_DIR, false);
setBondingStat(stat_creation_mutex, dent->d_name);
}
closedir(srcdir);
return 0;
}
static void
getStats(TSMutex stat_creation_mutex)
{
#ifdef HAVE_SYS_SYSINFO_H
struct sysinfo info;
sysinfo(&info);
statSet(LOAD_AVG_ONE_MIN, info.loads[0], stat_creation_mutex);
statSet(LOAD_AVG_FIVE_MIN, info.loads[1], stat_creation_mutex);
statSet(LOAD_AVG_FIFTEEN_MIN, info.loads[2], stat_creation_mutex);
statSet(CURRENT_PROCESSES, info.procs, stat_creation_mutex);
#endif // #ifdef HAVE_SYS_SYSINFO_H
netStatsInfo(stat_creation_mutex);
return;
}
static int
systemStatsContCB(TSCont cont, TSEvent event ATS_UNUSED, void *edata)
{
TSMutex stat_creation_mutex;
TSDebug(DEBUG_TAG, "entered %s", __FUNCTION__);
stat_creation_mutex = TSContMutexGet(cont);
getStats(stat_creation_mutex);
TSContScheduleOnPool(cont, SYSTEM_STATS_TIMEOUT, TS_THREAD_POOL_TASK);
TSDebug(DEBUG_TAG, "finished %s", __FUNCTION__);
return 0;
}
void
TSPluginInit(int argc, const char *argv[])
{
TSPluginRegistrationInfo info;
TSCont stats_cont;
info.plugin_name = PLUGIN_NAME;
info.vendor_name = "Apache Software Foundation";
info.support_email = "dev@trafficserver.apache.org";
if (TSPluginRegister(&info) != TS_SUCCESS) {
TSError("[%s] Plugin registration failed", DEBUG_TAG);
return;
} else {
TSDebug(DEBUG_TAG, "Plugin registration succeeded");
}
stats_cont = TSContCreate(systemStatsContCB, TSMutexCreate());
TSContDataSet(stats_cont, NULL);
// We want our first hit immediate to populate the stats,
// Subsequent schedules done within the function will be for
// 5 seconds.
TSContScheduleOnPool(stats_cont, 0, TS_THREAD_POOL_TASK);
TSDebug(DEBUG_TAG, "Init complete");
}
|