Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / mon / DataHealthService.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2013 Inktank, Inc
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation.  See file COPYING.
12  *
13  */
14 #include "include/memory.h"
15 #include <errno.h>
16 #include <map>
17 #include <list>
18 #include <string>
19 #include <sstream>
20
21 #include "acconfig.h"
22
23 #ifdef HAVE_SYS_VFS_H
24 #include <sys/vfs.h>
25 #endif
26
27 #ifdef HAVE_SYS_MOUNT_H
28 #include <sys/mount.h>
29 #endif
30
31 #ifdef HAVE_SYS_PARAM_H
32 #include <sys/param.h>
33 #endif
34
35 #include "messages/MMonHealth.h"
36 #include "include/assert.h"
37 #include "common/Formatter.h"
38 #include "common/errno.h"
39
40 #include "mon/Monitor.h"
41 #include "mon/DataHealthService.h"
42
43 #define dout_subsys ceph_subsys_mon
44 #undef dout_prefix
45 #define dout_prefix _prefix(_dout, mon, this)
46 static ostream& _prefix(std::ostream *_dout, const Monitor *mon,
47                         const DataHealthService *svc) {
48   assert(mon != NULL);
49   assert(svc != NULL);
50   return *_dout << "mon." << mon->name << "@" << mon->rank
51                 << "(" << mon->get_state_name() << ")." << svc->get_name()
52                 << "(" << svc->get_epoch() << ") ";
53 }
54
55 void DataHealthService::start_epoch()
56 {
57   dout(10) << __func__ << " epoch " << get_epoch() << dendl;
58   // we are not bound by election epochs, but we should clear the stats
59   // everytime an election is triggerd.  As far as we know, a monitor might
60   // have been running out of disk space and someone fixed it.  We don't want
61   // to hold the cluster back, even confusing the user, due to some possibly
62   // outdated stats.
63   stats.clear();
64   last_warned_percent = 0;
65 }
66
67 void DataHealthService::get_health(
68     list<pair<health_status_t,string> >& summary,
69     list<pair<health_status_t,string> > *detail)
70 {
71   dout(10) << __func__ << dendl;
72   for (map<entity_inst_t,DataStats>::iterator it = stats.begin();
73        it != stats.end(); ++it) {
74     string mon_name = mon->monmap->get_name(it->first.addr);
75     DataStats& stats = it->second;
76
77     health_status_t health_status = HEALTH_OK;
78     string health_detail;
79     if (stats.fs_stats.avail_percent <= g_conf->mon_data_avail_crit) {
80       health_status = HEALTH_ERR;
81       health_detail = "low disk space, shutdown imminent";
82     } else if (stats.fs_stats.avail_percent <= g_conf->mon_data_avail_warn) {
83       health_status = HEALTH_WARN;
84       health_detail = "low disk space";
85     }
86
87     if (stats.store_stats.bytes_total >= g_conf->mon_data_size_warn) {
88       if (health_status > HEALTH_WARN)
89         health_status = HEALTH_WARN;
90       if (!health_detail.empty())
91         health_detail.append("; ");
92       stringstream ss;
93       ss << "store is getting too big! "
94          << prettybyte_t(stats.store_stats.bytes_total)
95          << " >= " << prettybyte_t(g_conf->mon_data_size_warn);
96       health_detail.append(ss.str());
97     }
98
99     if (health_status != HEALTH_OK) {
100       stringstream ss;
101       ss << "mon." << mon_name << " " << health_detail;
102       summary.push_back(make_pair(health_status, ss.str()));
103       ss << " -- " <<  stats.fs_stats.avail_percent << "% avail";
104       if (detail)
105         detail->push_back(make_pair(health_status, ss.str()));
106     }
107   }
108 }
109
110 int DataHealthService::update_store_stats(DataStats &ours)
111 {
112   map<string,uint64_t> extra;
113   uint64_t store_size = mon->store->get_estimated_size(extra);
114   assert(store_size > 0);
115
116   ours.store_stats.bytes_total = store_size;
117   ours.store_stats.bytes_sst = extra["sst"];
118   ours.store_stats.bytes_log = extra["log"];
119   ours.store_stats.bytes_misc = extra["misc"];
120   ours.last_update = ceph_clock_now();
121
122   return 0;
123 }
124
125
126 int DataHealthService::update_stats()
127 {
128   entity_inst_t our_inst = mon->messenger->get_myinst();
129   DataStats& ours = stats[our_inst];
130
131   int err = get_fs_stats(ours.fs_stats, g_conf->mon_data.c_str());
132   if (err < 0) {
133     derr << __func__ << " get_fs_stats error: " << cpp_strerror(err) << dendl;
134     return err;
135   }
136   dout(0) << __func__ << " avail " << ours.fs_stats.avail_percent << "%"
137           << " total " << prettybyte_t(ours.fs_stats.byte_total)
138           << ", used " << prettybyte_t(ours.fs_stats.byte_used)
139           << ", avail " << prettybyte_t(ours.fs_stats.byte_avail) << dendl;
140   ours.last_update = ceph_clock_now();
141
142   return update_store_stats(ours);
143 }
144
145 void DataHealthService::share_stats()
146 {
147   dout(10) << __func__ << dendl;
148   if (!in_quorum())
149     return;
150
151   assert(!stats.empty());
152   entity_inst_t our_inst = mon->messenger->get_myinst();
153   assert(stats.count(our_inst) > 0);
154   DataStats &ours = stats[our_inst];
155   const set<int>& quorum = mon->get_quorum();
156   for (set<int>::const_iterator it = quorum.begin();
157        it != quorum.end(); ++it) {
158     if (mon->monmap->get_name(*it) == mon->name)
159       continue;
160     entity_inst_t inst = mon->monmap->get_inst(*it);
161     MMonHealth *m = new MMonHealth(HealthService::SERVICE_HEALTH_DATA,
162                                    MMonHealth::OP_TELL);
163     m->data_stats = ours;
164     dout(20) << __func__ << " send " << *m << " to " << inst << dendl;
165     mon->messenger->send_message(m, inst);
166   }
167 }
168
169 void DataHealthService::service_tick()
170 {
171   dout(10) << __func__ << dendl;
172
173   int err = update_stats();
174   if (err < 0) {
175     derr << "something went wrong obtaining our disk stats: "
176          << cpp_strerror(err) << dendl;
177     force_shutdown();
178     return;
179   }
180   if (in_quorum())
181     share_stats();
182
183   DataStats &ours = stats[mon->messenger->get_myinst()];
184
185   if (ours.fs_stats.avail_percent <= g_conf->mon_data_avail_crit) {
186     derr << "reached critical levels of available space on local monitor storage"
187          << " -- shutdown!" << dendl;
188     force_shutdown();
189     return;
190   }
191
192   // we must backoff these warnings, and track how much data is being
193   // consumed in-between reports to assess if it's worth to log this info,
194   // otherwise we may very well contribute to the consumption of the
195   // already low available disk space.
196   if (ours.fs_stats.avail_percent <= g_conf->mon_data_avail_warn) {
197     if (ours.fs_stats.avail_percent != last_warned_percent)
198       mon->clog->warn()
199         << "reached concerning levels of available space on local monitor storage"
200         << " (" << ours.fs_stats.avail_percent << "% free)";
201     last_warned_percent = ours.fs_stats.avail_percent;
202   } else {
203     last_warned_percent = 0;
204   }
205 }
206
207 void DataHealthService::handle_tell(MonOpRequestRef op)
208 {
209   op->mark_event("datahealth:handle_tell");
210   MMonHealth *m = static_cast<MMonHealth*>(op->get_req());
211   dout(10) << __func__ << " " << *m << dendl;
212   assert(m->get_service_op() == MMonHealth::OP_TELL);
213
214   stats[m->get_source_inst()] = m->data_stats;
215 }
216
217 bool DataHealthService::service_dispatch_op(MonOpRequestRef op)
218 {
219   op->mark_event("datahealth:service_dispatch_op");
220   MMonHealth *m = static_cast<MMonHealth*>(op->get_req());
221   dout(10) << __func__ << " " << *m << dendl;
222   assert(m->get_service_type() == get_type());
223   if (!in_quorum()) {
224     dout(1) << __func__ << " not in quorum -- drop message" << dendl;
225     return false;
226   }
227
228   switch (m->service_op) {
229     case MMonHealth::OP_TELL:
230       // someone is telling us their stats
231       handle_tell(op);
232       break;
233     default:
234       dout(0) << __func__ << " unknown op " << m->service_op << dendl;
235       assert(0 == "Unknown service op");
236       break;
237   }
238   return true;
239 }