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
|
/*
* Copyright (C) 2012-2013 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: Radek Novacek <rnovacek@redhat.com>
*/
#include "openlmi.h"
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <string.h>
#include <netdb.h>
#include <stdio.h>
#include <cmpimacs.h>
static char *_fqdn = NULL;
static int _log_level = _LMI_DEBUG_DEBUG;
static const CMPIBroker *_cb = NULL;
static char *_log_id = NULL;
static char *getFQDN(void)
{
struct utsname uts;
if ((uname(&uts) > 0) && (uts.nodename != NULL)) {
return strdup(uts.nodename);
}
char hostname[256];
hostname[255] = '\0';
if (gethostname(hostname, 255) == -1) {
// FIXME: what to do, if we can't use gethostname?
return NULL;
}
struct addrinfo hints;
struct addrinfo *info = NULL, *p;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // either IPV4 or IPV6
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_CANONNAME;
if (getaddrinfo(hostname, "http", &hints, &info) == 0) {
for (p = info; p != NULL; p = p->ai_next) {
if (p->ai_canonname && strstr(p->ai_canonname, "localhost") == NULL) {
char *dn = strdup(p->ai_canonname);
freeaddrinfo(info);
return dn;
}
}
}
if (info != NULL) {
freeaddrinfo(info);
}
return strdup(hostname);
}
const char *lmi_get_system_name()
{
if (_fqdn == NULL) {
_fqdn = getFQDN();
}
return _fqdn;
}
const char *lmi_get_system_creation_class_name()
{
return "Linux_ComputerSystem";
}
void lmi_init_logging(const char *log_id, const CMPIBroker *cb)
{
if (_log_id != NULL) {
free(_log_id);
}
_log_id = strdup(log_id);
_cb = cb;
}
int lmi_log_level(void)
{
return _log_level;
}
void lmi_set_log_level(int level)
{
_log_level = level;
}
void _lmi_debug(int level, const char *file, int line, const char *format, ...)
{
const char *lvl[] = { "NONE", "ERROR", "INFO", "WARNING", "DEBUG" };
if (level > 4) {
level = 4;
}
if (level < 1) {
level = 1;
}
char *message, *text;
va_list args;
va_start(args, format);
vasprintf(&message, format, args);
va_end(args);
asprintf(&text, "[%s] %s:%d\t%s", lvl[level], file, line, message);
free(message);
CMPIStatus rc;
rc.rc = CMPI_RC_OK;
if (_cb != NULL) {
// try to use standard CMPI logging
rc = _cb->eft->trace(_cb, CMPI_LEV_INFO, _log_id, text, NULL);
}
if (_cb == NULL || rc.rc != CMPI_RC_OK) {
// Fallback to stderr
if (level > _log_level) {
free(text);
return;
}
fprintf(stderr, "%s\n", text);
}
free(text);
}
|