summaryrefslogtreecommitdiffstats
path: root/src/hardware/LMI_DiskDriveProvider.c
blob: c71d075edabfe20bb67d3b2480dce3a575161ef0 (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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 * Copyright (C) 2013-2014 Red Hat, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Peter Schiffer <pschiffe@redhat.com>
 */

#include <unistd.h>
#include <limits.h>
#include <konkret/konkret.h>
#include "LMI_DiskDrive.h"
#include "utils.h"
#include "smartctl.h"
#include "lsblk.h"
#include "sysfs.h"

CMPIUint16 get_operational_status(const char *smart_status);
CMPIUint16 get_hdd_form_factor(const unsigned short form_factor);

static const CMPIBroker* _cb = NULL;

static void LMI_DiskDriveInitialize(const CMPIContext *ctx)
{
    lmi_init(provider_name, _cb, ctx, provider_config_defaults);
}

static CMPIStatus LMI_DiskDriveCleanup(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    CMPIBoolean term)
{
    CMReturn(CMPI_RC_OK);
}

static CMPIStatus LMI_DiskDriveEnumInstanceNames(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop)
{
    return KDefaultEnumerateInstanceNames(
        _cb, mi, cc, cr, cop);
}

static CMPIStatus LMI_DiskDriveEnumInstances(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const char** properties)
{
    LMI_DiskDrive lmi_hdd;
    const char *ns = KNameSpace(cop);
    unsigned i, j, rotational = 0, rpm;
    unsigned long capacity = 0;
    char instance_id[BUFLEN], path[PATH_MAX], *name;
    CMPIUint16 operational_status;
    SmartctlHdd *smtcl_hdds = NULL;
    unsigned smtcl_hdds_nb = 0;
    LsblkHdd *lsblk_hdds = NULL;
    unsigned lsblk_hdds_nb = 0;

    if (lsblk_get_hdds(&lsblk_hdds, &lsblk_hdds_nb) != 0 || lsblk_hdds_nb < 1) {
        goto done;
    }
    if (smartctl_get_hdds(&smtcl_hdds, &smtcl_hdds_nb) != 0 || smtcl_hdds_nb < 1) {
        smartctl_free_hdds(&smtcl_hdds, &smtcl_hdds_nb);
    }

    for (i = 0; i < lsblk_hdds_nb; i++) {
        /* use only disk devices from lsblk */
        if (strcmp(lsblk_hdds[i].type, "disk") != 0) {
            continue;
        }

        name = lsblk_hdds[i].name;
        capacity = lsblk_hdds[i].capacity;

        /* RPM value for unknown */
        rpm = 0xffffffff;

        LMI_DiskDrive_Init(&lmi_hdd, _cb, ns);

        LMI_DiskDrive_Set_SystemCreationClassName(&lmi_hdd,
                lmi_get_system_creation_class_name());
        LMI_DiskDrive_Set_SystemName(&lmi_hdd, lmi_get_system_name());
        LMI_DiskDrive_Set_CreationClassName(&lmi_hdd,
                LMI_DiskDrive_ClassName);
        LMI_DiskDrive_Set_Caption(&lmi_hdd, "Disk Drive");
        LMI_DiskDrive_Set_Description(&lmi_hdd,
                "This object represents one physical disk drive in system.");

        snprintf(instance_id, BUFLEN,
                LMI_ORGID ":" LMI_DiskDrive_ClassName ":%s",
                lsblk_hdds[i].name);

        LMI_DiskDrive_Set_DeviceID(&lmi_hdd, lsblk_hdds[i].name);
        LMI_DiskDrive_Set_InstanceID(&lmi_hdd, instance_id);

        /* check for smartctl output */
        for (j = 0; j < smtcl_hdds_nb; j++) {
            if (strcmp(smtcl_hdds[j].dev_path, lsblk_hdds[i].name) == 0) {
                operational_status = get_operational_status(
                        smtcl_hdds[j].smart_status);
                if (strlen(smtcl_hdds[j].name)) {
                    name = smtcl_hdds[j].name;
                } else if (strlen(smtcl_hdds[j].model)) {
                    name = smtcl_hdds[j].model;
                }
                if (!capacity) {
                    capacity = smtcl_hdds[j].capacity;
                }

                LMI_DiskDrive_Init_OperationalStatus(&lmi_hdd, 1);
                LMI_DiskDrive_Set_OperationalStatus(&lmi_hdd, 0, operational_status);
                if (operational_status == LMI_DiskDrive_OperationalStatus_OK) {
                    LMI_DiskDrive_Set_EnabledState(&lmi_hdd,
                            LMI_DiskDrive_EnabledState_Enabled);
                } else {
                    LMI_DiskDrive_Set_EnabledState(&lmi_hdd,
                            LMI_DiskDrive_EnabledState_Unknown);
                }

                LMI_DiskDrive_Set_FormFactor(&lmi_hdd,
                        get_hdd_form_factor(smtcl_hdds[j].form_factor));
                rpm = smtcl_hdds[j].rpm;
                if (smtcl_hdds[j].port_speed) {
                    LMI_DiskDrive_Set_InterconnectSpeed(&lmi_hdd,
                            smtcl_hdds[j].port_speed);
                }

                if (smtcl_hdds[j].curr_temp > SHRT_MIN) {
                    LMI_DiskDrive_Set_Temperature(&lmi_hdd,
                            smtcl_hdds[j].curr_temp);
                }

                break;
            }
        }

        LMI_DiskDrive_Set_Name(&lmi_hdd, name);
        LMI_DiskDrive_Set_ElementName(&lmi_hdd, name);
        LMI_DiskDrive_Set_Capacity(&lmi_hdd, capacity);

        snprintf(path, PATH_MAX, SYSFS_BLOCK_PATH "/%s/queue/rotational",
                lsblk_hdds[i].basename);
        if (path_get_unsigned(path, &rotational) == 0) {
            if (rotational == 1) {
                LMI_DiskDrive_Set_DiskType(&lmi_hdd,
                        LMI_DiskDrive_DiskType_Hard_Disk_Drive);
            } else if (rotational == 0) {
                LMI_DiskDrive_Set_DiskType(&lmi_hdd,
                        LMI_DiskDrive_DiskType_Solid_State_Drive);
                rpm = 0;
            } else {
                LMI_DiskDrive_Set_DiskType(&lmi_hdd,
                        LMI_DiskDrive_DiskType_Other);
            }
        } else {
            LMI_DiskDrive_Set_DiskType(&lmi_hdd,
                    LMI_DiskDrive_DiskType_Unknown);
        }

        LMI_DiskDrive_Set_RPM(&lmi_hdd, rpm);

        if (lsblk_hdds[i].basename[0] == 'h') {
            LMI_DiskDrive_Set_InterconnectType(&lmi_hdd,
                    LMI_DiskDrive_InterconnectType_ATA);
        } else if (lsblk_hdds[i].basename[0] == 's') {
            /* TODO: What about SAS? */
            LMI_DiskDrive_Set_InterconnectType(&lmi_hdd,
                    LMI_DiskDrive_InterconnectType_SATA);
        } else {
            LMI_DiskDrive_Set_InterconnectType(&lmi_hdd,
                    LMI_DiskDrive_InterconnectType_Other);
        }

        KReturnInstance(cr, lmi_hdd);
    }

done:
    smartctl_free_hdds(&smtcl_hdds, &smtcl_hdds_nb);
    lsblk_free_hdds(&lsblk_hdds, &lsblk_hdds_nb);

    CMReturn(CMPI_RC_OK);
}

static CMPIStatus LMI_DiskDriveGetInstance(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const char** properties)
{
    return KDefaultGetInstance(
        _cb, mi, cc, cr, cop, properties);
}

static CMPIStatus LMI_DiskDriveCreateInstance(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const CMPIInstance* ci)
{
    CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}

static CMPIStatus LMI_DiskDriveModifyInstance(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const CMPIInstance* ci,
    const char** properties)
{
    CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}

static CMPIStatus LMI_DiskDriveDeleteInstance(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop)
{
    CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}

static CMPIStatus LMI_DiskDriveExecQuery(
    CMPIInstanceMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const char* lang,
    const char* query)
{
    CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}

CMInstanceMIStub(
    LMI_DiskDrive,
    LMI_DiskDrive,
    _cb,
    LMI_DiskDriveInitialize(ctx))

static CMPIStatus LMI_DiskDriveMethodCleanup(
    CMPIMethodMI* mi,
    const CMPIContext* cc,
    CMPIBoolean term)
{
    CMReturn(CMPI_RC_OK);
}

static CMPIStatus LMI_DiskDriveInvokeMethod(
    CMPIMethodMI* mi,
    const CMPIContext* cc,
    const CMPIResult* cr,
    const CMPIObjectPath* cop,
    const char* meth,
    const CMPIArgs* in,
    CMPIArgs* out)
{
    return LMI_DiskDrive_DispatchMethod(
        _cb, mi, cc, cr, cop, meth, in, out);
}

CMMethodMIStub(
    LMI_DiskDrive,
    LMI_DiskDrive,
    _cb,
    LMI_DiskDriveInitialize(ctx))

KUint32 LMI_DiskDrive_RequestStateChange(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_DiskDriveRef* self,
    const KUint16* RequestedState,
    KRef* Job,
    const KDateTime* TimeoutPeriod,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

KUint32 LMI_DiskDrive_SetPowerState(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_DiskDriveRef* self,
    const KUint16* PowerState,
    const KDateTime* Time,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

KUint32 LMI_DiskDrive_Reset(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_DiskDriveRef* self,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

KUint32 LMI_DiskDrive_EnableDevice(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_DiskDriveRef* self,
    const KBoolean* Enabled,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

KUint32 LMI_DiskDrive_OnlineDevice(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_DiskDriveRef* self,
    const KBoolean* Online,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

KUint32 LMI_DiskDrive_QuiesceDevice(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_DiskDriveRef* self,
    const KBoolean* Quiesce,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

KUint32 LMI_DiskDrive_SaveProperties(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_DiskDriveRef* self,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

KUint32 LMI_DiskDrive_RestoreProperties(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_DiskDriveRef* self,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

KUint32 LMI_DiskDrive_LockMedia(
    const CMPIBroker* cb,
    CMPIMethodMI* mi,
    const CMPIContext* context,
    const LMI_DiskDriveRef* self,
    const KBoolean* Lock,
    CMPIStatus* status)
{
    KUint32 result = KUINT32_INIT;

    KSetStatus(status, ERR_NOT_SUPPORTED);
    return result;
}

/*
 * Get CIM operational status according to smart status.
 * @param smart_status from smartctl
 * @return CIM id of operational status
 */
CMPIUint16 get_operational_status(const char *smart_status)
{
    static struct {
        unsigned short cim_val;     /* CIM value */
        char *smart_status;         /* smart status */
    } values[] = {
        {LMI_DiskDrive_OperationalStatus_Unknown, "UNKNOWN!"},
        {LMI_DiskDrive_OperationalStatus_OK, "PASSED"},
        {LMI_DiskDrive_OperationalStatus_Predictive_Failure, "FAILED!"},
    };

    size_t i, val_length = sizeof(values) / sizeof(values[0]);

    for (i = 0; i < val_length; i++) {
        if (strcmp(smart_status, values[i].smart_status) == 0) {
            return values[i].cim_val;
        }
    }

    return LMI_DiskDrive_OperationalStatus_Unknown;
}

/*
 * Get CIM form factor according to reported disk value.
 * @param form_factor from smartctl
 * @return CIM id of form factor
 */
CMPIUint16 get_hdd_form_factor(const unsigned short form_factor)
{
    static struct {
        unsigned short cim_val;     /* CIM value */
        unsigned short form_factor; /* form factor */
    } values[] = {
        {LMI_DiskDrive_FormFactor_Not_Reported, 0},
        {LMI_DiskDrive_FormFactor_5_25_inch, 1},
        {LMI_DiskDrive_FormFactor_3_5_inch, 2},
        {LMI_DiskDrive_FormFactor_2_5_inch, 3},
        {LMI_DiskDrive_FormFactor_1_8_inch, 4},
    };

    size_t i, val_length = sizeof(values) / sizeof(values[0]);

    for (i = 0; i < val_length; i++) {
        if (form_factor == values[i].form_factor) {
            return values[i].cim_val;
        }
    }

    return LMI_DiskDrive_FormFactor_Other;
}

KONKRET_REGISTRATION(
    "root/cimv2",
    "LMI_DiskDrive",
    "LMI_DiskDrive",
    "instance method")