0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ServerMetrics.cc
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2012 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
26 
27 #include <Common/Compat.h>
28 #include "ServerMetrics.h"
29 
30 #include <Common/Error.h>
31 #include <Common/Logger.h>
32 
33 #include <boost/algorithm/string.hpp>
34 
35 using namespace Hypertable;
36 using namespace Hypertable::Lib::RS_METRICS;
37 using namespace std;
38 
39 ServerMeasurement::ServerMeasurement(const char *measurement, size_t len) {
40  memset(this, 0, sizeof(ServerMeasurement));
41  parse_measurement(measurement, len);
42 }
43 
44 void ServerMeasurement::parse_measurement(const char *measurement, size_t len) {
45  vector<String> splits;
46  String str(measurement, len);
47  boost::split(splits, str, boost::is_any_of(":,"));
48 
49  version = atoi(splits[0].c_str());
50  if (version == 2) {
51  if (splits.size() != 12)
53  format("Measurement string '%s' has %d components, expected 12.",
54  str.c_str(), (int)splits.size()));
55  }
56  else if (version == 3) {
57  if (splits.size() != 14)
59  format("Measurement string '%s' has %d components, expected 14.",
60  str.c_str(), (int)splits.size()));
61  }
62  else
64  format("ServerMetrics version=%d expected 2", (int)version));
65 
66 
67  timestamp = strtoll(splits[1].c_str(), 0, 0);
68  loadavg = strtod(splits[2].c_str(), 0);
69  disk_bytes_read_rate = strtod(splits[3].c_str(), 0);
70  bytes_written_rate = strtod(splits[4].c_str(), 0);
71  bytes_scanned_rate = strtod(splits[5].c_str(), 0);
72  updates_rate = strtod(splits[6].c_str(), 0);
73  scans_rate = strtod(splits[7].c_str(), 0);
74  cells_written_rate = strtod(splits[8].c_str(), 0);
75  cells_scanned_rate = strtod(splits[9].c_str(), 0);
76  page_in = strtod(splits[10].c_str(), 0);
77  page_out = strtod(splits[11].c_str(), 0);
78  if (version == 3) {
79  disk_total = strtoll(splits[12].c_str(), 0, 0);
80  disk_avail = strtoll(splits[13].c_str(), 0, 0);
81  }
82 }
83 
84 void ServerMetrics::add_measurement(const char *measurement, size_t len) {
85  try {
86  ServerMeasurement sm(measurement, len);
87  m_measurements.push_back(sm);
88  }
89  catch (Exception &e) {
90  if (e.code() == Error::NOT_IMPLEMENTED) {
91  HT_WARN_OUT << e << HT_END;
92  }
93  else
94  HT_THROW(e.code(), e.what());
95  }
96 }
97 
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
String format(const char *fmt,...)
Returns a String using printf like format facilities Vanilla snprintf is about 1.5x faster than this...
Definition: String.cc:37
Facilities for reading and writing sys/RS_METRICS table.
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
Declarations for ServerMetrics.
STL namespace.
Logging routines and macros.
Compatibility Macros for C/C++.
#define HT_END
Definition: Logger.h:220
#define HT_WARN_OUT
Definition: Logger.h:291
Hypertable definitions
void parse_measurement(const char *measurement, size_t len)
bool split(int flags)
Tests the SPLIT bit of flags
This is a generic exception class for Hypertable.
Definition: Error.h:314
Error codes, Exception handling, error logging.
#define HT_THROW(_code_, _msg_)
Definition: Error.h:478
void add_measurement(const char *measurement, size_t len)
Single server metrics measurement.
Definition: ServerMetrics.h:43
int code() const
Returns the error code.
Definition: Error.h:391