summaryrefslogtreecommitdiff
path: root/src/pmmgr/pmmgr.h
blob: 4934ace889a81b4b4aeb0035f51302b70a02219e (plain)
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
/* -*- C++ -*-
 * Copyright (c) 2013-2014 Red Hat.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#ifndef PMMGR_H
#define PMMGR_H

extern "C" {
#include "pmapi.h"
}
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stdexcept>
#include <iostream>


typedef std::string pcp_context_spec; // pmNewContext PM_CONTEXT_HOST parameter
typedef std::string pmmgr_hostid; // a unique id for a pmcd


// Instances of pmmgr_configurable represent a configurable object,
// which reads one or more lines of djb-style directories. 
class pmmgr_configurable
{
protected:
  pmmgr_configurable(const std::string& dir);
  virtual ~pmmgr_configurable() {}

  std::vector<std::string> get_config_multi(const std::string&) const;
  std::string get_config_single(const std::string&) const;
  bool get_config_exists(const std::string&) const;

  // private: maybe?
  std::string config_directory;

  std::ostream& timestamp(std::ostream&);
  int wrap_system(const std::string& cmd);
};




// Instances of pmmgr_daemon represent a possibly-live, restartable daemon.
class pmmgr_daemon: public pmmgr_configurable 
{
public:
  pmmgr_daemon(const std::string& config_directory, 
               const pmmgr_hostid& hostid, const pcp_context_spec& spec);
  virtual ~pmmgr_daemon();
  void poll();

protected:
  pmmgr_hostid hostid;
  pcp_context_spec spec;
  int pid;
  time_t last_restart_attempt;

  virtual std::string daemon_command_line() = 0;
};


class pmmgr_pmlogger_daemon: public pmmgr_daemon
{
public:
  pmmgr_pmlogger_daemon(const std::string& config_directory, 
                        const pmmgr_hostid& hostid, const pcp_context_spec& spec);
protected:
  std::string daemon_command_line();
};

class pmmgr_pmie_daemon: public pmmgr_daemon
{
public:
  pmmgr_pmie_daemon(const std::string& config_directory, 
                    const pmmgr_hostid& hostid, const pcp_context_spec& spec);
protected:
  std::string daemon_command_line();
};




// An instance of a pmmgr_job_spec represents a pmmgr
// configuration item to monitor some set of pcp target patterns
// (which collectively map to a varying set of pmcd's), and a
// corresponding set of daemons to keep running for each of them.
//
// The pmcds are identified by a configurable algorithm that collects
// site-specific metrics into a single string, which is then sanitized
// to make it typeable, portable, useful as a directory name.
//
// It is configured from a djb-style control directory with files containing
// 100% pure content.  Multiple values within the files, where permitted,
// are newline-separated.

class pmmgr_job_spec: pmmgr_configurable
{
public:
  pmmgr_job_spec(const std::string& config_directory);
  ~pmmgr_job_spec(); // shut down all daemons
  void poll(); // check targets, daemons

private:
  std::map<std::string,pmMetricSpec*> parsed_metric_cache;
  pmMetricSpec* parse_metric_spec(const std::string&);

  pmmgr_hostid compute_hostid (const pcp_context_spec&);
  std::map<pmmgr_hostid,pcp_context_spec> known_targets;

  void note_new_hostid(const pmmgr_hostid&, const pcp_context_spec&);
  void note_dead_hostid(const pmmgr_hostid&);
  std::multimap<pmmgr_hostid,pmmgr_daemon*> daemons;
};






#endif