-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathsysinfo.c
3640 lines (3113 loc) · 122 KB
/
sysinfo.c
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2024 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
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; version 3.
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <platform.h>
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>
#include <sysinfo.h>
#include <sysinfo_priv.h>
#include <cf3.extern.h>
#include <eval_context.h>
#include <files_names.h>
#include <files_interfaces.h>
#include <hash.h>
#include <scope.h>
#include <item_lib.h>
#include <matching.h>
#include <systype.h>
#include <unix.h>
#include <string_lib.h>
#include <regex.h> /* StringMatchFull */
#include <misc_lib.h>
#include <file_lib.h>
#include <rlist.h>
#include <audit.h>
#include <pipes.h>
#include <known_dirs.h>
#include <files_lib.h>
#include <printsize.h>
#include <cf-windows-functions.h>
#include <ornaments.h>
#include <feature.h>
#include <evalfunction.h>
#include <json-utils.h>
#include <unix.h> /* GetCurrentUserName() */
#ifdef HAVE_ZONE_H
# include <zone.h>
#endif
// HP-UX mpctl() for $(sys.cpus) on HP-UX - Mantis #1069
#ifdef HAVE_SYS_MPCTL_H
# include <sys/mpctl.h>
#endif
/* Linux.
WARNING keep this before the #include <sys/sysctl.h> because of glibc bug:
https://sourceware.org/bugzilla/show_bug.cgi?id=140 */
#ifdef HAVE_STRUCT_SYSINFO_UPTIME
# include <sys/sysinfo.h>
#endif
/* BSD, MAC OS X uptime calculation use KERN_BOOTTIME sysctl. */
#ifdef HAVE_SYS_SYSCTL_H
# ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
# endif
# ifdef __linux__
# include <linux/sysctl.h>
# else
# include <sys/sysctl.h>
# endif
#endif
/*****************************************************/
// Uptime calculation settings for GetUptimeSeconds() - Mantis #1134
/* Listed here in priority order, i.e. first come the platform-specific
* ways. If nothing works, one of the last, most generic ways should. */
#ifndef __MINGW32__ /* Windows is implemented in Enterprise */
// HP-UX: pstat_getproc(2) on init (pid 1)
#if defined(__hpux)
# include <sys/param.h>
# include <sys/pstat.h>
# define BOOT_TIME_WITH_PSTAT_GETPROC
// Solaris: kstat() for kernel statistics
// See http://dsc.sun.com/solaris/articles/kstatc.html
// BSD also has a kstat.h (albeit in sys), so check __sun just to be paranoid
/**
* @WARNING: Commented out because inside a Solaris 10 zone this gives the
* uptime of the host machine (the hypervisor). We thus choose to
* use UTMP for Solaris.
*/
/*
#elif defined(__sun) && defined(HAVE_KSTAT_H)
# include <kstat.h>
# define BOOT_TIME_WITH_KSTAT
*/
// BSD: sysctl(3) to get kern.boottime, CPU count, etc.
// See http://www.unix.com/man-page/FreeBSD/3/sysctl/
// Linux also has sys/sysctl.h, so we check KERN_BOOTTIME to make sure it's BSD
#elif defined(HAVE_SYS_SYSCTL_H) && defined(KERN_BOOTTIME)
# define BOOT_TIME_WITH_SYSCTL
// GNU/Linux: struct sysinfo.uptime
#elif defined(HAVE_STRUCT_SYSINFO_UPTIME)
# define BOOT_TIME_WITH_SYSINFO
/* Generic System V way, available in most platforms. */
#elif defined(HAVE_UTMP_H)
# include <utmp.h>
# define BOOT_TIME_WITH_UTMP
/* POSIX alternative (utmp.h does not exist on BSDs). */
#elif defined(HAVE_UTMPX_H)
# include <utmpx.h>
# define BOOT_TIME_WITH_UTMPX
#else
// Most generic way: {stat("/proc/1")}.st_ctime
// TODO in Solaris zones init is not guaranteed to be PID 1!
#define BOOT_TIME_WITH_PROCFS
#endif
/* Fallback uptime calculation: Parse the "uptime" command in case the
* platform-specific way fails or returns absurd number. */
static time_t GetBootTimeFromUptimeCommand(time_t now);
#endif /* ifndef __MINGW32__ */
#define LSB_RELEASE_FILENAME "/etc/lsb-release"
#define DEBIAN_VERSION_FILENAME "/etc/debian_version"
#define DEBIAN_ISSUE_FILENAME "/etc/issue"
/*****************************************************/
static char *FindNextInteger(char *str, char **num);
void CalculateDomainName(const char *nodename, const char *dnsname,
char *fqname, size_t fqname_size,
char *uqname, size_t uqname_size,
char *domain, size_t domain_size);
#ifdef __linux__
static int Linux_Fedora_Version(EvalContext *ctx);
static int Linux_Redhat_Version(EvalContext *ctx);
static void Linux_Amazon_Version(EvalContext *ctx);
static void Linux_Alpine_Version(EvalContext *ctx);
static void Linux_Oracle_VM_Server_Version(EvalContext *ctx);
static void Linux_Oracle_Version(EvalContext *ctx);
static int Linux_Suse_Version(EvalContext *ctx);
static int Linux_Slackware_Version(EvalContext *ctx, char *filename);
static int Linux_Debian_Version(EvalContext *ctx);
static int Linux_Misc_Version(EvalContext *ctx);
static int Linux_Mandrake_Version(EvalContext *ctx);
static int Linux_Mandriva_Version(EvalContext *ctx);
static int Linux_Mandriva_Version_Real(EvalContext *ctx, char *filename, char *relstring, char *vendor);
static int VM_Version(EvalContext *ctx);
static int Xen_Domain(EvalContext *ctx);
static int EOS_Version(EvalContext *ctx);
static int MiscOS(EvalContext *ctx);
static void OpenVZ_Detect(EvalContext *ctx);
static bool ReadLine(const char *filename, char *buf, int bufsize);
static FILE *ReadFirstLine(const char *filename, char *buf, int bufsize);
#endif
#ifdef XEN_CPUID_SUPPORT
static void Xen_Cpuid(uint32_t idx, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx);
static bool Xen_Hv_Check(void);
#endif
static void GetCPUInfo(EvalContext *ctx);
static const char *const CLASSATTRIBUTES[][3] =
{
[PLATFORM_CONTEXT_UNKNOWN] = {"-", "-", "-"}, /* as appear here are matched. The fields are sysname and machine */
[PLATFORM_CONTEXT_OPENVZ] = {"virt_host_vz_vzps", ".*", ".*"}, /* VZ with vzps */
[PLATFORM_CONTEXT_HP] = {"hp-ux", ".*", ".*"}, /* hpux */
[PLATFORM_CONTEXT_AIX] = {"aix", ".*", ".*"}, /* aix */
[PLATFORM_CONTEXT_LINUX] = {"linux", ".*", ".*"}, /* linux */
[PLATFORM_CONTEXT_BUSYBOX] = {"busybox", ".*", ".*"}, /* linux w/ busybox - warning uname returns linux */
[PLATFORM_CONTEXT_SOLARIS] = {"sunos", ".*",
"5\\.1[1-9].*"}, /* new solaris, SunOS >= 5.11 */
[PLATFORM_CONTEXT_SUN_SOLARIS] = {"sunos", ".*",
"5\\.([2-9]|10)(\\..*)?"}, /* old solaris, SunOS < 5.11 */
[PLATFORM_CONTEXT_FREEBSD] = {"freebsd", ".*", ".*"}, /* freebsd */
[PLATFORM_CONTEXT_NETBSD] = {"netbsd", ".*", ".*"}, /* NetBSD */
[PLATFORM_CONTEXT_CRAYOS] = {"sn.*", "cray*", ".*"}, /* cray */
[PLATFORM_CONTEXT_WINDOWS_NT] = {"cygwin_nt.*", ".*", ".*"}, /* NT (cygwin) */
[PLATFORM_CONTEXT_SYSTEMV] = {"unix_sv", ".*", ".*"}, /* Unixware */
[PLATFORM_CONTEXT_OPENBSD] = {"openbsd", ".*", ".*"}, /* OpenBSD */
[PLATFORM_CONTEXT_CFSCO] = {"sco_sv", ".*", ".*"}, /* SCO */
[PLATFORM_CONTEXT_DARWIN] = {"darwin", ".*", ".*"}, /* Darwin, aka MacOS X */
[PLATFORM_CONTEXT_QNX] = {"qnx", ".*", ".*"}, /* qnx */
[PLATFORM_CONTEXT_DRAGONFLY] = {"dragonfly", ".*", ".*"}, /* dragonfly */
[PLATFORM_CONTEXT_MINGW] = {"windows_nt.*", ".*", ".*"}, /* NT (native) */
[PLATFORM_CONTEXT_VMWARE] = {"vmkernel", ".*", ".*"}, /* VMWARE / ESX */
[PLATFORM_CONTEXT_ANDROID] = {"android", ".*", ".*"}, /* android: Warning uname returns linux */
};
static const char *const VRESOLVCONF[] =
{
[PLATFORM_CONTEXT_UNKNOWN] = "-",
[PLATFORM_CONTEXT_OPENVZ] = "/etc/resolv.conf", /* virt_host_vz_vzps */
[PLATFORM_CONTEXT_HP] = "/etc/resolv.conf", /* hpux */
[PLATFORM_CONTEXT_AIX] = "/etc/resolv.conf", /* aix */
[PLATFORM_CONTEXT_LINUX] = "/etc/resolv.conf", /* linux */
[PLATFORM_CONTEXT_BUSYBOX] = "/etc/resolv.conf", /* linux */
[PLATFORM_CONTEXT_SOLARIS] = "/etc/resolv.conf", /* new solaris */
[PLATFORM_CONTEXT_SUN_SOLARIS] = "/etc/resolv.conf", /* old solaris */
[PLATFORM_CONTEXT_FREEBSD] = "/etc/resolv.conf", /* freebsd */
[PLATFORM_CONTEXT_NETBSD] = "/etc/resolv.conf", /* netbsd */
[PLATFORM_CONTEXT_CRAYOS] = "/etc/resolv.conf", /* cray */
[PLATFORM_CONTEXT_WINDOWS_NT] = "/etc/resolv.conf", /* NT */
[PLATFORM_CONTEXT_SYSTEMV] = "/etc/resolv.conf", /* Unixware */
[PLATFORM_CONTEXT_OPENBSD] = "/etc/resolv.conf", /* openbsd */
[PLATFORM_CONTEXT_CFSCO] = "/etc/resolv.conf", /* sco */
[PLATFORM_CONTEXT_DARWIN] = "/etc/resolv.conf", /* darwin */
[PLATFORM_CONTEXT_QNX] = "/etc/resolv.conf", /* qnx */
[PLATFORM_CONTEXT_DRAGONFLY] = "/etc/resolv.conf", /* dragonfly */
[PLATFORM_CONTEXT_MINGW] = "", /* mingw */
[PLATFORM_CONTEXT_VMWARE] = "/etc/resolv.conf", /* vmware */
[PLATFORM_CONTEXT_ANDROID] = "", /* android */
};
static const char *const VMAILDIR[] =
{
[PLATFORM_CONTEXT_UNKNOWN] = "-",
[PLATFORM_CONTEXT_OPENVZ] = "/var/spool/mail", /* virt_host_vz_vzps */
[PLATFORM_CONTEXT_HP] = "/var/mail", /* hpux */
[PLATFORM_CONTEXT_AIX] = "/var/spool/mail", /* aix */
[PLATFORM_CONTEXT_LINUX] = "/var/spool/mail", /* linux */
[PLATFORM_CONTEXT_BUSYBOX] = "", /* linux */
[PLATFORM_CONTEXT_SOLARIS] = "/var/mail", /* new solaris */
[PLATFORM_CONTEXT_SUN_SOLARIS] = "/var/mail", /* old solaris */
[PLATFORM_CONTEXT_FREEBSD] = "/var/mail", /* freebsd */
[PLATFORM_CONTEXT_NETBSD] = "/var/mail", /* netbsd */
[PLATFORM_CONTEXT_CRAYOS] = "/usr/mail", /* cray */
[PLATFORM_CONTEXT_WINDOWS_NT] = "N/A", /* NT */
[PLATFORM_CONTEXT_SYSTEMV] = "/var/mail", /* Unixware */
[PLATFORM_CONTEXT_OPENBSD] = "/var/mail", /* openbsd */
[PLATFORM_CONTEXT_CFSCO] = "/var/spool/mail", /* sco */
[PLATFORM_CONTEXT_DARWIN] = "/var/mail", /* darwin */
[PLATFORM_CONTEXT_QNX] = "/var/spool/mail", /* qnx */
[PLATFORM_CONTEXT_DRAGONFLY] = "/var/mail", /* dragonfly */
[PLATFORM_CONTEXT_MINGW] = "", /* mingw */
[PLATFORM_CONTEXT_VMWARE] = "/var/spool/mail", /* vmware */
[PLATFORM_CONTEXT_ANDROID] = "", /* android */
};
static const char *const VEXPORTS[] =
{
[PLATFORM_CONTEXT_UNKNOWN] = "-",
[PLATFORM_CONTEXT_OPENVZ] = "/etc/exports", /* virt_host_vz_vzps */
[PLATFORM_CONTEXT_HP] = "/etc/exports", /* hpux */
[PLATFORM_CONTEXT_AIX] = "/etc/exports", /* aix */
[PLATFORM_CONTEXT_LINUX] = "/etc/exports", /* linux */
[PLATFORM_CONTEXT_BUSYBOX] = "", /* linux */
[PLATFORM_CONTEXT_SOLARIS] = "/etc/dfs/dfstab", /* new solaris */
[PLATFORM_CONTEXT_SUN_SOLARIS] = "/etc/dfs/dfstab", /* old solaris */
[PLATFORM_CONTEXT_FREEBSD] = "/etc/exports", /* freebsd */
[PLATFORM_CONTEXT_NETBSD] = "/etc/exports", /* netbsd */
[PLATFORM_CONTEXT_CRAYOS] = "/etc/exports", /* cray */
[PLATFORM_CONTEXT_WINDOWS_NT] = "/etc/exports", /* NT */
[PLATFORM_CONTEXT_SYSTEMV] = "/etc/dfs/dfstab", /* Unixware */
[PLATFORM_CONTEXT_OPENBSD] = "/etc/exports", /* openbsd */
[PLATFORM_CONTEXT_CFSCO] = "/etc/dfs/dfstab", /* sco */
[PLATFORM_CONTEXT_DARWIN] = "/etc/exports", /* darwin */
[PLATFORM_CONTEXT_QNX] = "/etc/exports", /* qnx */
[PLATFORM_CONTEXT_DRAGONFLY] = "/etc/exports", /* dragonfly */
[PLATFORM_CONTEXT_MINGW] = "", /* mingw */
[PLATFORM_CONTEXT_VMWARE] = "none", /* vmware */
[PLATFORM_CONTEXT_ANDROID] = "" , /* android */
};
/*******************************************************************/
void CalculateDomainName(const char *nodename, const char *dnsname,
char *fqname, size_t fqname_size,
char *uqname, size_t uqname_size,
char *domain, size_t domain_size)
{
if (strstr(dnsname, "."))
{
strlcpy(fqname, dnsname, fqname_size);
}
else
{
strlcpy(fqname, nodename, fqname_size);
}
if ((strncmp(nodename, fqname, strlen(nodename)) == 0) && (fqname[strlen(nodename)] == '.'))
{
/* If hostname is not qualified */
strlcpy(domain, fqname + strlen(nodename) + 1, domain_size);
strlcpy(uqname, nodename, uqname_size);
}
else
{
/* If hostname is qualified */
char *p = strchr(nodename, '.');
if (p != NULL)
{
strlcpy(uqname, nodename, MIN(uqname_size, p - nodename + 1));
strlcpy(domain, p + 1, domain_size);
}
else
{
strlcpy(uqname, nodename, uqname_size);
strlcpy(domain, "", domain_size);
}
}
}
/*******************************************************************/
void DetectDomainName(EvalContext *ctx, const char *orig_nodename)
{
char nodename[CF_BUFSIZE];
strlcpy(nodename, orig_nodename, sizeof(nodename));
ToLowerStrInplace(nodename);
char dnsname[CF_BUFSIZE] = "";
char fqn[CF_BUFSIZE];
if (gethostname(fqn, sizeof(fqn)) != -1)
{
struct hostent *hp;
if ((hp = gethostbyname(fqn)))
{
strlcpy(dnsname, hp->h_name, sizeof(dnsname));
ToLowerStrInplace(dnsname);
}
}
nt_static_assert(sizeof(VFQNAME) > 255);
nt_static_assert(sizeof(VUQNAME) > 255);
nt_static_assert(sizeof(VDOMAIN) > 255);
CalculateDomainName(nodename, dnsname, VFQNAME, sizeof(VFQNAME),
VUQNAME, sizeof(VUQNAME), VDOMAIN, sizeof(VDOMAIN));
// Note: We don't expect hostnames or domain names above 255
// Not supported by DNS:
// https://tools.ietf.org/html/rfc2181#section-11
// So let's print some warnings:
if (strlen(VUQNAME) > 255)
{
Log(LOG_LEVEL_WARNING,
"Long host name '%s' (%zu bytes) will may cause issues",
VUQNAME,
strlen(VUQNAME));
}
if (strlen(VDOMAIN) > 255)
{
Log(LOG_LEVEL_WARNING,
"Long domain name '%s' (%zu bytes) will may cause issues",
VDOMAIN,
strlen(VDOMAIN));
}
/*
* VFQNAME = a.b.c.d ->
* NewClass("a.b.c.d")
* NewClass("b.c.d")
* NewClass("c.d")
* NewClass("d")
*/
char *ptr = VFQNAME;
do
{
EvalContextClassPutHard(ctx, ptr, "inventory,attribute_name=none,source=agent,derived-from=sys.fqhost");
ptr = strchr(ptr, '.');
if (ptr != NULL)
{
ptr++;
}
} while (ptr != NULL);
EvalContextClassPutHard(ctx, VUQNAME, "source=agent,derived-from=sys.uqhost");
EvalContextClassPutHard(ctx, VDOMAIN, "source=agent,derived-from=sys.domain");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "host", nodename, CF_DATA_TYPE_STRING, "inventory,source=agent,attribute_name=none");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "uqhost", VUQNAME, CF_DATA_TYPE_STRING, "inventory,source=agent,attribute_name=none");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "fqhost", VFQNAME, CF_DATA_TYPE_STRING, "inventory,source=agent,attribute_name=Host name");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "domain", VDOMAIN, CF_DATA_TYPE_STRING, "source=agent");
}
/*******************************************************************/
void DiscoverVersion(EvalContext *ctx)
{
int major = 0;
int minor = 0;
int patch = 0;
char workbuf[CF_BUFSIZE];
if (sscanf(Version(), "%d.%d.%d", &major, &minor, &patch) == 3)
{
snprintf(workbuf, CF_MAXVARSIZE, "%d", major);
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "cf_version_major", workbuf, CF_DATA_TYPE_STRING, "source=agent");
snprintf(workbuf, CF_MAXVARSIZE, "%d", minor);
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "cf_version_minor", workbuf, CF_DATA_TYPE_STRING, "source=agent");
snprintf(workbuf, CF_MAXVARSIZE, "%d", patch);
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "cf_version_patch", workbuf, CF_DATA_TYPE_STRING, "source=agent");
}
else
{
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "cf_version_major", "BAD VERSION " VERSION, CF_DATA_TYPE_STRING, "source=agent");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "cf_version_minor", "BAD VERSION " VERSION, CF_DATA_TYPE_STRING, "source=agent");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "cf_version_patch", "BAD VERSION " VERSION, CF_DATA_TYPE_STRING, "source=agent");
}
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "cf_version_release", RELEASE, CF_DATA_TYPE_STRING, "source=agent");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "local_libdir", "lib", CF_DATA_TYPE_STRING, "source=agent");
snprintf(workbuf, CF_BUFSIZE, "%s%cinputs%clib", GetWorkDir(), FILE_SEPARATOR, FILE_SEPARATOR);
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "libdir", workbuf, CF_DATA_TYPE_STRING, "source=agent");
}
static void GetNameInfo3(EvalContext *ctx)
{
int i;
char *sp, workbuf[CF_BUFSIZE];
time_t tloc;
struct hostent *hp;
struct sockaddr_in cin;
unsigned char digest[EVP_MAX_MD_SIZE + 1];
const char* const workdir = GetWorkDir();
const char* const bindir = GetBinDir();
#ifdef _AIX
char real_version[_SYS_NMLN];
#endif
#if defined(HAVE_SYSINFO) && (defined(SI_ARCHITECTURE) || defined(SI_PLATFORM))
long sz;
#endif
#define COMPONENTS_SIZE 17
// This is used for $(sys.cf_agent), $(sys.cf_serverd) ... :
char *components[COMPONENTS_SIZE] = { "cf-twin", "cf-agent", "cf-serverd", "cf-monitord", "cf-know",
"cf-report", "cf-key", "cf-runagent", "cf-execd", "cf-hub", "cf-reactor",
"cf-promises", "cf-upgrade", "cf-net", "cf-check", "cf-secret",
NULL
};
int have_component[COMPONENTS_SIZE] = {0};
struct stat sb;
char name[CF_MAXVARSIZE], quoteName[CF_MAXVARSIZE + 2], shortname[CF_MAXVARSIZE];
if (uname(&VSYSNAME) == -1)
{
Log(LOG_LEVEL_ERR, "Couldn't get kernel name info!. (uname: %s)", GetErrorStr());
memset(&VSYSNAME, 0, sizeof(VSYSNAME));
}
#ifdef _AIX
snprintf(real_version, _SYS_NMLN, "%.80s.%.80s", VSYSNAME.version, VSYSNAME.release);
strlcpy(VSYSNAME.release, real_version, _SYS_NMLN);
#endif
#if defined(__ANDROID__) && !defined(__TERMUX__)
/*
* uname cannot differentiate android from linux
*/
strcpy(VSYSNAME.sysname, "android");
#endif
#ifdef __BUSYBOX__
/*
* uname cannot differentiate a busybox toolset from a normal GNU linux toolset
*/
strcpy(VSYSNAME.sysname, "busybox");
#endif
ToLowerStrInplace(VSYSNAME.sysname);
ToLowerStrInplace(VSYSNAME.machine);
#ifdef _AIX
switch (_system_configuration.architecture)
{
case POWER_RS:
strlcpy(VSYSNAME.machine, "power", _SYS_NMLN);
break;
case POWER_PC:
strlcpy(VSYSNAME.machine, "powerpc", _SYS_NMLN);
break;
case IA64:
strlcpy(VSYSNAME.machine, "ia64", _SYS_NMLN);
break;
}
#endif
/*
* solarisx86 is a historically defined class for Solaris on x86. We have to
* define it manually now.
*/
#ifdef __sun
if (strcmp(VSYSNAME.machine, "i86pc") == 0)
{
EvalContextClassPutHard(ctx, "solarisx86", "inventory,attribute_name=none,source=agent");
}
#endif
DetectDomainName(ctx, VSYSNAME.nodename);
if ((tloc = time((time_t *) NULL)) == -1)
{
Log(LOG_LEVEL_ERR, "Couldn't read system clock");
}
else
{
snprintf(workbuf, CF_BUFSIZE, "%jd", (intmax_t) tloc);
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "systime", workbuf, CF_DATA_TYPE_INT, "time_based,source=agent");
snprintf(workbuf, CF_BUFSIZE, "%jd", (intmax_t) tloc / SECONDS_PER_DAY);
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "sysday", workbuf, CF_DATA_TYPE_INT, "time_based,source=agent");
i = GetUptimeMinutes(tloc);
if (i != -1)
{
snprintf(workbuf, CF_BUFSIZE, "%d", i);
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "uptime", workbuf, CF_DATA_TYPE_INT, "inventory,time_based,source=agent,attribute_name=Uptime minutes");
}
}
bool found = false;
for (i = 0; !found && (i < PLATFORM_CONTEXT_MAX); i++)
{
char sysname[CF_BUFSIZE];
strlcpy(sysname, VSYSNAME.sysname, CF_BUFSIZE);
ToLowerStrInplace(sysname);
/* FIXME: review those strcmps. Moved out from StringMatch */
if (!strcmp(CLASSATTRIBUTES[i][0], sysname)
|| StringMatchFull(CLASSATTRIBUTES[i][0], sysname))
{
if (!strcmp(CLASSATTRIBUTES[i][1], VSYSNAME.machine)
|| StringMatchFull(CLASSATTRIBUTES[i][1], VSYSNAME.machine))
{
if (!strcmp(CLASSATTRIBUTES[i][2], VSYSNAME.release)
|| StringMatchFull(CLASSATTRIBUTES[i][2], VSYSNAME.release))
{
EvalContextClassPutHard(ctx, CLASSTEXT[i], "inventory,attribute_name=none,source=agent,derived-from=sys.class");
found = true;
VSYSTEMHARDCLASS = (PlatformContext) i;
VPSHARDCLASS = (PlatformContext) i; /* this one can be overriden at vz detection */
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "class", CLASSTEXT[i], CF_DATA_TYPE_STRING, "inventory,source=agent,attribute_name=Kernel");
}
}
else
{
Log(LOG_LEVEL_DEBUG, "I recognize '%s' but not '%s'", VSYSNAME.sysname, VSYSNAME.machine);
continue;
}
}
}
if (!found)
{
i = 0;
}
Log(LOG_LEVEL_VERBOSE, "%s - ready", NameVersion());
Banner("Environment discovery");
snprintf(workbuf, CF_BUFSIZE, "%s", CLASSTEXT[i]); // See: cppcheck_suppressions.txt
Log(LOG_LEVEL_VERBOSE, "Host name is: %s", VSYSNAME.nodename);
Log(LOG_LEVEL_VERBOSE, "Operating System Type is %s", VSYSNAME.sysname);
Log(LOG_LEVEL_VERBOSE, "Operating System Release is %s", VSYSNAME.release);
Log(LOG_LEVEL_VERBOSE, "Architecture = %s", VSYSNAME.machine);
Log(LOG_LEVEL_VERBOSE, "CFEngine detected operating system description is %s", workbuf);
Log(LOG_LEVEL_VERBOSE, "The time is now %s", ctime(&tloc));
snprintf(workbuf, CF_MAXVARSIZE, "%s", ctime(&tloc));
Chop(workbuf, CF_EXPANDSIZE);
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "date", workbuf, CF_DATA_TYPE_STRING, "time_based,source=agent");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "cdate", CanonifyName(workbuf), CF_DATA_TYPE_STRING, "time_based,source=agent");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "os", VSYSNAME.sysname, CF_DATA_TYPE_STRING, "source=agent");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "release", VSYSNAME.release, CF_DATA_TYPE_STRING, "inventory,source=agent,attribute_name=Kernel Release");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "version", VSYSNAME.version, CF_DATA_TYPE_STRING, "source=agent");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "arch", VSYSNAME.machine, CF_DATA_TYPE_STRING, "inventory,source=agent,attribute_name=Architecture");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "workdir", workdir, CF_DATA_TYPE_STRING, "source=agent");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "fstab", VFSTAB[VSYSTEMHARDCLASS], CF_DATA_TYPE_STRING, "source=agent");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "resolv", VRESOLVCONF[VSYSTEMHARDCLASS], CF_DATA_TYPE_STRING, "source=agent");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "maildir", VMAILDIR[VSYSTEMHARDCLASS], CF_DATA_TYPE_STRING, "source=agent");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "exports", VEXPORTS[VSYSTEMHARDCLASS], CF_DATA_TYPE_STRING, "source=agent");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "logdir", GetLogDir(), CF_DATA_TYPE_STRING, "source=agent");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "piddir", GetPidDir(), CF_DATA_TYPE_STRING, "source=agent");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "statedir", GetStateDir(), CF_DATA_TYPE_STRING, "source=agent");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "masterdir", GetMasterDir(), CF_DATA_TYPE_STRING, "source=agent");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "inputdir", GetInputDir(), CF_DATA_TYPE_STRING, "source=agent");
snprintf(workbuf, CF_BUFSIZE, "%s", bindir);
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "bindir", workbuf, CF_DATA_TYPE_STRING, "source=agent");
snprintf(workbuf, CF_BUFSIZE, "%s%cpromises.cf", GetInputDir(), FILE_SEPARATOR);
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "default_policy_path", workbuf, CF_DATA_TYPE_STRING, "source=agent");
snprintf(workbuf, CF_BUFSIZE, "%s%cfailsafe.cf", GetInputDir(), FILE_SEPARATOR);
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "failsafe_policy_path", workbuf, CF_DATA_TYPE_STRING, "source=agent");
snprintf(workbuf, CF_BUFSIZE, "%s%cupdate.cf", GetInputDir(), FILE_SEPARATOR);
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "update_policy_path", workbuf, CF_DATA_TYPE_STRING, "source=agent");
/* FIXME: type conversion */
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "cf_version", (char *) Version(), CF_DATA_TYPE_STRING, "inventory,source=agent,attribute_name=CFEngine version");
DiscoverVersion(ctx);
if (PUBKEY)
{
char pubkey_digest[CF_HOSTKEY_STRING_SIZE] = { 0 };
HashPubKey(PUBKEY, digest, CF_DEFAULT_DIGEST);
HashPrintSafe(pubkey_digest, sizeof(pubkey_digest), digest,
CF_DEFAULT_DIGEST, true);
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "key_digest", pubkey_digest, CF_DATA_TYPE_STRING, "inventory,source=agent,attribute_name=CFEngine ID");
snprintf(workbuf, CF_MAXVARSIZE - 1, "PK_%s", pubkey_digest);
CanonifyNameInPlace(workbuf);
EvalContextClassPutHard(ctx, workbuf, "inventory,attribute_name=none,source=agent,derived-from=sys.key_digest");
}
for (i = 0; components[i] != NULL; i++)
{
snprintf(shortname, CF_MAXVARSIZE - 1, "%s", CanonifyName(components[i]));
#if defined(_WIN32)
// twin has own dir, and is named agent
if (i == 0)
{
snprintf(name, CF_MAXVARSIZE - 1, "%s-twin%ccf-agent.exe", bindir, FILE_SEPARATOR);
}
else
{
snprintf(name, CF_MAXVARSIZE - 1, "%s%c%s.exe", bindir, FILE_SEPARATOR, components[i]);
}
#else
snprintf(name, CF_MAXVARSIZE - 1, "%s%c%s", bindir, FILE_SEPARATOR, components[i]);
#endif
have_component[i] = false;
if (stat(name, &sb) != -1)
{
snprintf(quoteName, sizeof(quoteName), "\"%s\"", name);
// Sets $(sys.cf_agent), $(sys.cf_serverd) $(sys.cf_execd) etc.
// to their respective /bin paths
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, shortname, quoteName, CF_DATA_TYPE_STRING, "cfe_internal,source=agent");
have_component[i] = true;
}
}
// If no twin, fail over the agent
if (!have_component[0])
{
snprintf(shortname, CF_MAXVARSIZE - 1, "%s", CanonifyName(components[0]));
#if defined(_WIN32)
snprintf(name, CF_MAXVARSIZE - 1, "%s%c%s.exe", bindir, FILE_SEPARATOR,
components[1]);
#else
snprintf(name, CF_MAXVARSIZE - 1, "%s%c%s", bindir, FILE_SEPARATOR, components[1]);
#endif
if (stat(name, &sb) != -1)
{
snprintf(quoteName, sizeof(quoteName), "\"%s\"", name);
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, shortname, quoteName, CF_DATA_TYPE_STRING, "cfe_internal,source=agent");
}
}
/* Windows special directories and tools */
#ifdef __MINGW32__
if (NovaWin_GetWinDir(workbuf, sizeof(workbuf)))
{
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "windir", workbuf, CF_DATA_TYPE_STRING, "source=agent");
}
if (NovaWin_GetSysDir(workbuf, sizeof(workbuf)))
{
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "winsysdir", workbuf, CF_DATA_TYPE_STRING, "source=agent");
char filename[CF_BUFSIZE];
if (snprintf(filename, sizeof(filename), "%s%s", workbuf, "\\WindowsPowerShell\\v1.0\\powershell.exe") < sizeof(filename))
{
if (NovaWin_FileExists(filename))
{
EvalContextClassPutHard(ctx, "powershell", "inventory,attribute_name=none,source=agent");
Log(LOG_LEVEL_VERBOSE, "Additional hard class defined as: %s", "powershell");
}
}
}
if (NovaWin_GetProgDir(workbuf, sizeof(workbuf)))
{
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "winprogdir", workbuf, CF_DATA_TYPE_STRING, "source=agent");
}
# ifdef _WIN64
// only available on 64 bit windows systems
if (NovaWin_GetEnv("PROGRAMFILES(x86)", workbuf, sizeof(workbuf)))
{
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "winprogdir86", workbuf, CF_DATA_TYPE_STRING, "source=agent");
}
# else/* NOT _WIN64 */
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "winprogdir86", "", CF_DATA_TYPE_STRING, "source=agent");
# endif
#endif /* !__MINGW32__ */
EnterpriseContext(ctx);
snprintf(workbuf, sizeof(workbuf), "%u_bit", (unsigned) sizeof(void*) * 8);
EvalContextClassPutHard(ctx, workbuf, "source=agent");
Log(LOG_LEVEL_VERBOSE, "Additional hard class defined as: %s", CanonifyName(workbuf));
snprintf(workbuf, CF_BUFSIZE, "%s_%s", VSYSNAME.sysname, VSYSNAME.release);
EvalContextClassPutHard(ctx, workbuf, "inventory,attribute_name=none,source=agent,derived-from=sys.sysname,derived-from=sys.release");
EvalContextClassPutHard(ctx, VSYSNAME.machine, "source=agent,derived-from=sys.machine");
Log(LOG_LEVEL_VERBOSE, "Additional hard class defined as: %s", CanonifyName(workbuf));
snprintf(workbuf, CF_BUFSIZE, "%s_%s", VSYSNAME.sysname, VSYSNAME.machine);
EvalContextClassPutHard(ctx, workbuf, "source=agent,derived-from=sys.sysname,derived-from=sys.machine");
Log(LOG_LEVEL_VERBOSE, "Additional hard class defined as: %s", CanonifyName(workbuf));
snprintf(workbuf, CF_BUFSIZE, "%s_%s_%s", VSYSNAME.sysname, VSYSNAME.machine, VSYSNAME.release);
EvalContextClassPutHard(ctx, workbuf, "source=agent,derived-from=sys.sysname,derived-from=sys.machine,derived-from=sys.release");
Log(LOG_LEVEL_VERBOSE, "Additional hard class defined as: %s", CanonifyName(workbuf));
#ifdef HAVE_SYSINFO
# ifdef SI_ARCHITECTURE
sz = sysinfo(SI_ARCHITECTURE, workbuf, CF_BUFSIZE);
if (sz == -1)
{
Log(LOG_LEVEL_VERBOSE, "cfengine internal: sysinfo returned -1");
}
else
{
EvalContextClassPutHard(ctx, workbuf, "inventory,attribute_name=none,source=agent");
Log(LOG_LEVEL_VERBOSE, "Additional hard class defined as: %s", workbuf);
}
# endif
# ifdef SI_PLATFORM
sz = sysinfo(SI_PLATFORM, workbuf, CF_BUFSIZE);
if (sz == -1)
{
Log(LOG_LEVEL_VERBOSE, "cfengine internal: sysinfo returned -1");
}
else
{
EvalContextClassPutHard(ctx, workbuf, "inventory,attribute_name=none,source=agent");
Log(LOG_LEVEL_VERBOSE, "Additional hard class defined as: %s", workbuf);
}
# endif
#endif
snprintf(workbuf, CF_BUFSIZE, "%s_%s_%s_%s", VSYSNAME.sysname, VSYSNAME.machine, VSYSNAME.release,
VSYSNAME.version);
if (strlen(workbuf) > CF_MAXVARSIZE - 2)
{
Log(LOG_LEVEL_VERBOSE, "cfengine internal: $(arch) overflows CF_MAXVARSIZE! Truncating");
}
sp = xstrdup(CanonifyName(workbuf));
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "long_arch", sp, CF_DATA_TYPE_STRING, "source=agent");
EvalContextClassPutHard(ctx, sp, "source=agent,derived-from=sys.long_arch");
free(sp);
snprintf(workbuf, CF_BUFSIZE, "%s_%s", VSYSNAME.sysname, VSYSNAME.machine);
sp = xstrdup(CanonifyName(workbuf));
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "ostype", sp, CF_DATA_TYPE_STRING, "source=agent");
EvalContextClassPutHard(ctx, sp, "inventory,attribute_name=none,source=agent,derived-from=sys.ostype");
free(sp);
if (!found)
{
Log(LOG_LEVEL_ERR, "I don't understand what architecture this is");
}
char compile_str[] = "compiled_on_";
size_t compile_str_len = sizeof(compile_str);
strcpy(workbuf, compile_str);
strlcat(workbuf, CanonifyName(AUTOCONF_SYSNAME),
CF_BUFSIZE - compile_str_len);
EvalContextClassPutHard(ctx, workbuf, "source=agent");
Log(LOG_LEVEL_VERBOSE, "GNU autoconf class from compile time: %s", workbuf);
/* Get IP address from nameserver */
if ((hp = gethostbyname(VFQNAME)) == NULL)
{
Log(LOG_LEVEL_VERBOSE, "Hostname lookup failed on node name '%s'", VSYSNAME.nodename);
return;
}
else
{
memset(&cin, 0, sizeof(cin));
cin.sin_addr.s_addr = ((struct in_addr *) (hp->h_addr))->s_addr;
Log(LOG_LEVEL_VERBOSE, "Address given by nameserver: %s", inet_ntoa(cin.sin_addr));
strcpy(VIPADDRESS, inet_ntoa(cin.sin_addr));
for (i = 0; hp->h_aliases[i] != NULL; i++)
{
Log(LOG_LEVEL_DEBUG, "Adding alias '%s'", hp->h_aliases[i]);
EvalContextClassPutHard(ctx, hp->h_aliases[i], "inventory,attribute_name=none,source=agent,based-on=sys.fqhost");
}
}
#ifdef HAVE_GETZONEID
zoneid_t zid;
char zone[ZONENAME_MAX];
char vbuff[CF_BUFSIZE];
zid = getzoneid();
getzonenamebyid(zid, zone, ZONENAME_MAX);
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "zone", zone, CF_DATA_TYPE_STRING, "inventory,source=agent,attribute_name=Solaris zone");
snprintf(vbuff, CF_BUFSIZE - 1, "zone_%s", zone);
EvalContextClassPutHard(ctx, vbuff, "source=agent,derived-from=sys.zone");
if (strcmp(zone, "global") == 0)
{
Log(LOG_LEVEL_VERBOSE, "CFEngine seems to be running inside a global solaris zone of name '%s'", zone);
}
else
{
Log(LOG_LEVEL_VERBOSE, "CFEngine seems to be running inside a local solaris zone of name '%s'", zone);
}
#endif
}
/*******************************************************************/
void LoadSlowlyVaryingObservations(EvalContext *ctx)
{
CF_DB *dbp;
CF_DBC *dbcp;
char *key;
void *stored;
int ksize, vsize;
if (!OpenDB(&dbp, dbid_static))
{
return;
}
/* Acquire a cursor for the database. */
if (!NewDBCursor(dbp, &dbcp))
{
Log(LOG_LEVEL_INFO, "Unable to scan class db");
CloseDB(dbp);
return;
}
while (NextDB(dbcp, &key, &ksize, &stored, &vsize))
{
if (key == NULL || stored == NULL)
{
continue;
}
char lval[1024];
int type_i;
int ret = sscanf(key, "%1023[^:]:%d", lval, &type_i);
if (ret == 2)
{
DataType type = type_i;
switch (type)
{
case CF_DATA_TYPE_STRING:
case CF_DATA_TYPE_INT:
case CF_DATA_TYPE_REAL:
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_MON,
lval, stored, type,
"monitoring,source=observation");
break;
case CF_DATA_TYPE_STRING_LIST:
{
Rlist *list = RlistFromSplitString(stored, ',');
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_MON,
lval, list, CF_DATA_TYPE_STRING_LIST,
"monitoring,source=observation");
RlistDestroy(list);
break;
}
case CF_DATA_TYPE_COUNTER:
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_MON,
lval, stored, CF_DATA_TYPE_STRING,
"monitoring,source=observation");
break;
default:
Log(LOG_LEVEL_ERR,
"Unexpected monitoring type %d found in dbid_static database",
(int) type);
}
}
}
DeleteDBCursor(dbcp);
CloseDB(dbp);
}
static void Get3Environment(EvalContext *ctx)
{
char env[CF_BUFSIZE], context[CF_BUFSIZE], name[CF_MAXVARSIZE], value[CF_BUFSIZE];
struct stat statbuf;
time_t now = time(NULL);
Log(LOG_LEVEL_VERBOSE, "Looking for environment from cf-monitord...");
snprintf(env, CF_BUFSIZE, "%s/%s", GetStateDir(), CF_ENV_FILE);
MapName(env);
FILE *fp = safe_fopen(env, "r");
if (fp == NULL)
{
Log(LOG_LEVEL_VERBOSE, "Unable to detect environment from cf-monitord");
return;
}
int fd = fileno(fp);
if (fstat(fd, &statbuf) == -1)
{
Log(LOG_LEVEL_VERBOSE, "Unable to detect environment from cf-monitord");
fclose(fp);
return;
}
if (statbuf.st_mtime < (now - 60 * 60))
{
Log(LOG_LEVEL_VERBOSE, "Environment data are too old - discarding");
unlink(env);
fclose(fp);
return;
}
snprintf(value, CF_MAXVARSIZE - 1, "%s", ctime(&statbuf.st_mtime));
if (Chop(value, CF_EXPANDSIZE) == -1)
{
Log(LOG_LEVEL_ERR, "Chop was called on a string that seemed to have no terminator");
}
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_MON, "env_time", value, CF_DATA_TYPE_STRING, "time_based,source=agent");
Log(LOG_LEVEL_VERBOSE, "Loading environment...");
for(;;)
{
name[0] = '\0';
value[0] = '\0';
if (fgets(context, sizeof(context), fp) == NULL)
{
if (ferror(fp))
{
UnexpectedError("Failed to read line from stream");
break;
}
else /* feof */
{
break;
}