File: Display.cpp

package info (click to toggle)
drbd-utils 9.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,388 kB
  • sloc: ansic: 43,698; xml: 15,968; cpp: 7,783; sh: 3,699; makefile: 1,353; perl: 353
file content (402 lines) | stat: -rw-r--r-- 13,285 bytes parent folder | download
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include <GenericDisplay.h>
#include <Display.h>
#include <DrbdMon.h>

const char* Display::ANSI_CLEAR = "\x1b[f\x1b[0J\x1b[f";
const char* Display::ANSI_CLEAR_LINE = "\x1b[K";

// Format of the header line
const char* Display::FORMAT_HEADER = "\x1b[1;37;44m";

// Resource formats
const char* Display::RES_LABEL       = "\x1b[0;30;42mRES:\x1b[0m";
const char* Display::RES_FORMAT_NAME = "\x1b[0;4;32m";

// Role formats
const char* Display::ROLE_LABEL            = "\x1b[0m";
const char* Display::ROLE_FORMAT_PRIMARY   = "\x1b[0;1;36m";
const char* Display::ROLE_FORMAT_SECONDARY = "\x1b[0;36m";

// Connection formats
const char* Display::CONN_LABEL             = "\x1b[0;37;44mP:\x1b[0m";
const char* Display::CONN_FORMAT_NAME       = "\x1b[0;4;37m";
const char* Display::CONN_FORMAT_NODE_ID    = "\x1b[0m";
const char* Display::CONN_FORMAT_STATE_NORM = "\x1b[0;32m";

// Volume formats
const char* Display::VOL_LABEL               = "\x1b[0;30;46mV:\x1b[0m";
const char* Display::VOL_LABEL_MINOR         = "\x1b[0mminor #";
const char* Display::VOL_FORMAT_NR           = "\x1b[0;32m";
const char* Display::VOL_FORMAT_MINOR        = "\x1b[0;36m";
const char* Display::VOL_FORMAT_STATE_NORM   = "\x1b[0;1;32m";
const char* Display::VOL_FORMAT_STATE_CLIENT = "\x1b[0;1;33;44m";
const char* Display::VOL_FORMAT_REPL_NORM    = "\x1b[0;32m";

// Generic formats
const char* Display::FORMAT_WARN  = "\x1b[0;30;43m";
const char* Display::FORMAT_ALERT = "\x1b[1;33;41m";
const char* Display::FORMAT_RESET = "\x1b[0m";

const uint16_t Display::MIN_SIZE_X =   40;
const uint16_t Display::MAX_SIZE_X = 1024;
const uint16_t Display::MIN_SIZE_Y =   15;
const uint16_t Display::MAX_SIZE_Y = 1024;

Display::Display(std::ostream& out_ref, ResourcesMap& resources_map_ref) :
    resources_map(resources_map_ref),
    out(out_ref)
{
}

void Display::clear()
{
    out << ANSI_CLEAR;
}

void Display::initial_display()
{
    clear();
    out << "Reading initial DRBD status" << std::endl;
}

void Display::status_display()
{
    clear();
    if (show_header)
    {
        display_header();
    }
    list_resources();
}

void Display::display_header() const
{
    if (show_header)
    {
        out << FORMAT_HEADER << ANSI_CLEAR_LINE <<
            DrbdMon::PROGRAM_NAME << " v" << DrbdMon::VERSION
            << std::endl;
    }
}

void Display::list_resources()
{
    ResourcesMap::ValuesIterator iter(resources_map);
    size_t count = iter.get_size();
    for (size_t index = 0; index < count; ++index)
    {
        DrbdResource& res = *(iter.next());

        DrbdRole::resource_role role = res.get_role();
        const char* role_label = res.get_role_label();
        const std::string& name = res.get_name();

        const char* role_format = FORMAT_ALERT;
        if (role == DrbdRole::resource_role::PRIMARY)
        {
            role_format = ROLE_FORMAT_PRIMARY;
        }
        else
        if (role == DrbdRole::resource_role::SECONDARY)
        {
            role_format = ROLE_FORMAT_SECONDARY;
        }

        out << RES_LABEL << RES_FORMAT_NAME << std::left << std::setw(32) << name.c_str() <<
            FORMAT_RESET << "       " <<
            role_format << std::setw(15) << role_label << FORMAT_RESET << std::endl;

        list_volumes(res);
        list_connections(res);
    }
    if (count == 0)
    {
        out << RES_FORMAT_NAME << "No active DRBD resources.\n" << std::endl;
    }
}

void Display::list_connections(DrbdResource& res)
{
    ConnectionsMap::ValuesIterator iter = res.connections_iterator();
    size_t count = iter.get_size();
    for (size_t index = 0; index < count; ++index)
    {
        DrbdConnection& conn = *(iter.next());

        bool display_peer_volumes = true;

        DrbdRole::resource_role role = conn.get_role();
        DrbdConnection::state state = conn.get_connection_state();
        const char* role_label = conn.get_role_label();
        const char* state_label = conn.get_connection_state_label();
        const std::string& name = conn.get_name();

        const char* conn_format = FORMAT_ALERT;
        if (state == DrbdConnection::state::CONNECTED)
        {
            conn_format = CONN_FORMAT_STATE_NORM;
        }

        // Do not display the list of peer volumes if it is not
        // surprising that they are all in an unknown state
        switch (state)
        {
            case DrbdConnection::state::NETWORK_FAILURE:
                // fall-through
            case DrbdConnection::state::PROTOCOL_ERROR:
                // fall-through
            case DrbdConnection::state::STANDALONE:
                // fall-through
            case DrbdConnection::state::TEAR_DOWN:
                // fall-through
            case DrbdConnection::state::CONNECTING:
                // fall-through
            case DrbdConnection::state::UNCONNECTED:
                display_peer_volumes = false;
                break;
            case DrbdConnection::state::DISCONNECTING:
                // fall-through
            case DrbdConnection::state::TIMEOUT:
                // fall-through
            case DrbdConnection::state::BROKEN_PIPE:
                // fall-through
            case DrbdConnection::state::CONNECTED:
                // fall-through
            case DrbdConnection::state::UNKNOWN:
                // fall-through
            default:
                break;
        }

        const char* role_format = FORMAT_ALERT;
        if (role == DrbdRole::resource_role::PRIMARY)
        {
            role_format = ROLE_FORMAT_PRIMARY;
        }
        else
        if (role == DrbdRole::resource_role::SECONDARY)
        {
            role_format = ROLE_FORMAT_SECONDARY;
        }
        else
        {
            // Do not mark an unknown role as an alert if
            // the role is unknown because the network connection
            // is faulty
            switch (state)
            {
                case DrbdConnection::state::BROKEN_PIPE:
                    // fall-through
                case DrbdConnection::state::DISCONNECTING:
                    // fall-through
                case DrbdConnection::state::NETWORK_FAILURE:
                    // fall-through
                case DrbdConnection::state::PROTOCOL_ERROR:
                    // fall-through
                case DrbdConnection::state::TEAR_DOWN:
                    // fall-through
                case DrbdConnection::state::TIMEOUT:
                    // fall-through
                case DrbdConnection::state::UNCONNECTED:
                    role_format = FORMAT_WARN;
                    break;
                case DrbdConnection::state::CONNECTING:
                    // fall-through
                case DrbdConnection::state::STANDALONE:
                    role_format = ROLE_FORMAT_SECONDARY;
                    break;
                case DrbdConnection::state::CONNECTED:
                    // fall-through
                case DrbdConnection::state::UNKNOWN:
                    // fall-through
                default:
                    // All other states are marked as an alert
                    break;
            }
        }

        out << "    " <<
            CONN_LABEL << CONN_FORMAT_NAME << std::left << std::setw(20) << name.c_str() <<
            FORMAT_RESET << " " <<
            conn_format << std::left << std::setw(15) << state_label <<
            FORMAT_RESET << " " <<
            role_format << std::left << std::setw(10) << role_label <<
            FORMAT_RESET << std::endl;

        if (display_peer_volumes)
        {
            list_peer_volumes(conn);
        }
    }
}

void Display::list_volumes(DrbdResource& res)
{
    VolumesMap::ValuesIterator iter = res.volumes_iterator();
    size_t count = iter.get_size();
    for (size_t index = 0; index < count; ++index)
    {
        DrbdVolume& vol = *(iter.next());

        uint16_t vol_nr = vol.get_volume_nr();
        int32_t minor_nr = vol.get_minor_nr();
        DrbdVolume::disk_state disk_state = vol.get_disk_state();
        const char* disk_label = vol.get_disk_state_label();

        const char* disk_format = FORMAT_ALERT;
        if (disk_state == DrbdVolume::disk_state::UP_TO_DATE)
        {
            disk_format = VOL_FORMAT_STATE_NORM;
        }
        else
        if (disk_state == DrbdVolume::disk_state::DISKLESS)
        {
            disk_format = VOL_FORMAT_STATE_CLIENT;
        }

        out << "    " <<
            VOL_LABEL << VOL_FORMAT_NR <<
            std::right << std::setw(2) << static_cast<long> (vol_nr) <<
            FORMAT_RESET << " " <<
            disk_format << std::left << std::setw(14) << disk_label <<
            FORMAT_RESET << "    " <<
            VOL_LABEL_MINOR << VOL_FORMAT_MINOR << std::right << std::setw(4) << static_cast<long> (minor_nr) <<
            FORMAT_RESET << std::endl;
    }
}

void Display::list_peer_volumes(DrbdConnection& conn)
{
    VolumesMap::ValuesIterator iter = conn.volumes_iterator();
    size_t count = iter.get_size();
    for (size_t index = 0; index < count; ++index)
    {
        DrbdVolume& vol = *(iter.next());

        DrbdVolume::disk_state disk_state = vol.get_disk_state();
        DrbdVolume::repl_state repl_state = vol.get_replication_state();

        bool disk_state_norm = (disk_state == DrbdVolume::disk_state::UP_TO_DATE ||
                                disk_state == DrbdVolume::disk_state::DISKLESS);
        bool repl_state_norm = (repl_state == DrbdVolume::repl_state::ESTABLISHED);
        if (!hide_norm_peer_volumes || !disk_state_norm || !repl_state_norm)
        {
            uint16_t vol_nr = vol.get_volume_nr();
            const char* disk_label = vol.get_disk_state_label();
            const char* repl_label = vol.get_replication_state_label();

            const char* disk_format = FORMAT_ALERT;
            if (disk_state == DrbdVolume::disk_state::UP_TO_DATE)
            {
                disk_format = VOL_FORMAT_STATE_NORM;
            }
            else
            if (disk_state == DrbdVolume::disk_state::DISKLESS)
            {
                disk_format = VOL_FORMAT_STATE_CLIENT;
            }

            bool show_replication = true;
            const char* repl_format = FORMAT_ALERT;
            switch (repl_state)
            {
                case DrbdVolume::repl_state::ESTABLISHED:
                    show_replication = false;
                    break;
                case DrbdVolume::repl_state::SYNC_SOURCE:
                    // fall-through
                case DrbdVolume::repl_state::SYNC_TARGET:
                    // fall-through
                    repl_format = VOL_FORMAT_REPL_NORM;
                    break;
                case DrbdVolume::repl_state::STARTING_SYNC_SOURCE:
                    // fall-through
                case DrbdVolume::repl_state::STARTING_SYNC_TARGET:
                    // fall-through
                case DrbdVolume::repl_state::WF_BITMAP_SOURCE:
                    // fall-through
                case DrbdVolume::repl_state::WF_BITMAP_TARGET:
                    // fall-through
                case DrbdVolume::repl_state::WF_SYNC_UUID:
                    // fall-through
                case DrbdVolume::repl_state::AHEAD:
                    // fall-through
                case DrbdVolume::repl_state::BEHIND:
                    repl_format = FORMAT_WARN;
                    break;
                case DrbdVolume::repl_state::OFF:
                    // fall-through
                case DrbdVolume::repl_state::PAUSED_SYNC_SOURCE:
                    // fall-through
                case DrbdVolume::repl_state::PAUSED_SYNC_TARGET:
                    // fall-through
                case DrbdVolume::repl_state::VERIFY_SOURCE:
                    // fall-through
                case DrbdVolume::repl_state::VERIFY_TARGET:
                    // fall-through
                case DrbdVolume::repl_state::UNKNOWN:
                    // fall-through
                default:
                    // all other states are marked as an alert
                    break;
            }

            out << "        " <<
                VOL_LABEL << VOL_FORMAT_NR <<
                std::right << std::setw(2) << static_cast<long> (vol_nr) <<
                FORMAT_RESET << " " <<
                disk_format << std::left << std::setw(15) << disk_label <<
                FORMAT_RESET << " ";

            if (show_replication)
            {
                out << repl_format << std::left << std::setw(15) << repl_label;
            }
            else
            {
                out << std::setw(15) << "";
            }

            out << FORMAT_RESET << std::endl;
        }
    }
}

void Display::set_terminal_size(uint16_t size_x, uint16_t size_y)
{
    if (size_x >= MIN_SIZE_X)
    {
        if (size_x <= MAX_SIZE_X)
        {
            term_x = size_x;
        }
        else
        {
            term_x = MAX_SIZE_X;
        }
    }
    else
    {
        term_x = MIN_SIZE_X;
    }

    if (size_y >= MIN_SIZE_Y)
    {
        if (size_y <= MAX_SIZE_Y)
        {
            term_y = size_y;
        }
        else
        {
            term_y = MAX_SIZE_Y;
        }
    }
    else
    {
        term_y = MIN_SIZE_Y;
    }
}

void Display::key_pressed(const char key)
{
}