1bundle common inventory_linux 2# @brief Linux inventory 3# 4# This common bundle is for Linux inventory work. 5# 6# Provides: 7# os_release_id, and os_release_version based on parsing of /etc/os-release 8# systemd class based on linktarget of /proc/1/cmdline 9{ 10 vars: 11 has_os_release:: 12 "os_release_info" string => readfile("/etc/os-release", "512"), 13 comment => "Read /etc/os-release" ; 14 15 os_release_has_id:: 16 "os_release_id" string => canonify("$(id_array[1])"); 17 18 os_release_has_version:: 19 "os_release_version" string => canonify("$(version_array[1])"); 20 21 has_proc_1_cmdline:: 22 "proc_1_cmdline_split" slist => string_split(readfile("/proc/1/cmdline", "512"), " ", "2"), 23 comment => "Read /proc/1/cmdline and split off arguments"; 24 25 "proc_1_cmdline" string => nth("proc_1_cmdline_split", 0), 26 comment => "Get argv[0] of /proc/1/cmdline"; 27 28 # this is the same as the original file for non-links 29 "proc_1_process" string => filestat($(proc_1_cmdline), "linktarget"); 30 31 any:: 32 "proc_routes" data => data_readstringarrayidx("/proc/net/route", 33 "#[^\n]*","\s+",40,4k), 34 if => fileexists("/proc/net/route"); 35 "routeidx" slist => getindices("proc_routes"); 36 "dgw_ipv4_iface" string => "$(proc_routes[$(routeidx)][0])", 37 comment => "Name of the interface where default gateway is routed", 38 if => strcmp("$(proc_routes[$(routeidx)][1])", "00000000"); 39 40 linux:: 41 "nfs_servers" -> { "CFE-3259" } 42 comment => "NFS servers (to list hosts impacted by NFS outages)", 43 slist => maplist( regex_replace( $(this) , ":.*", "", "g"), 44 # NFS server is before the colon (:), that's all we want 45 # e.g., nfs.example.com:/vol/homedir/user1 /home/user1 ... 46 # ^^^^^^^^^^^^^^^ 47 grep( ".* nfs .*", 48 readstringlist("/proc/mounts", "", "\n", inf, inf) 49 ) 50 ), 51 if => fileexists( "/proc/mounts" ); 52 53 54 "nfs_server[$(nfs_servers)]" 55 string => "$(nfs_servers)", 56 meta => { "inventory", "attribute_name=NFS Server" }; 57 58 59 classes: 60 61 any:: 62 "has_os_release" expression => fileexists("/etc/os-release"), 63 comment => "Check if we can get more info from /etc/os-release"; 64 65 "os_release_has_id" expression => regextract('^ID="?([^"\s]+)"?$', 66 $(os_release_info), 67 "id_array"), 68 comment => "Extract ID= line from os-release to id_array"; 69 70 "os_release_has_version" expression => regextract('^VERSION_ID="?([^"]+)"?$', 71 $(os_release_info), 72 "version_array"), 73 comment => "Extract VERSION_ID= line from os-release to version_array"; 74 75 "has_proc_1_cmdline" expression => fileexists("/proc/1/cmdline"), 76 comment => "Check if we can read /proc/1/cmdline"; 77 78 os_release_has_id:: 79 "$(os_release_id)" expression => "any"; 80 81 os_release_has_version:: 82 "$(os_release_id)_$(os_release_version)" expression => "any"; 83 84 has_proc_1_cmdline:: 85 "systemd" expression => strcmp(lastnode($(proc_1_process), "/"), "systemd"), 86 comment => "Check if (the link target of) /proc/1/cmdline is systemd"; 87 88 reports: 89 (DEBUG|DEBUG_inventory_linux).has_os_release:: 90 "DEBUG $(this.bundle)"; 91 "$(const.t)OS release ID = $(os_release_id), OS release version = $(os_release_version)"; 92} 93