MCPcopy Index your code
hub / github.com/PatchMon/PatchMon / DetectOS

Method DetectOS

agent-source-code/internal/system/system.go:165–232  ·  view source on GitHub ↗

DetectOS detects the operating system and version using /etc/os-release

()

Source from the content-addressed store, hash-verified

163
164// DetectOS detects the operating system and version using /etc/os-release
165func (d *Detector) DetectOS() (osType, osVersion string, err error) {
166 // Check for Windows first (uses gopsutil)
167 if runtime.GOOS == "windows" {
168 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
169 defer cancel()
170 info, infoErr := host.InfoWithContext(ctx)
171 if infoErr != nil {
172 return "Windows", "Unknown", nil
173 }
174 osVer := info.PlatformVersion
175 if osVer == "" {
176 osVer = "Unknown"
177 }
178 return "Windows", osVer, nil
179 }
180 // Check for FreeBSD first (doesn't have /etc/os-release)
181 if d.isFreeBSD() {
182 if d.isPfSense() {
183 return d.getPfSenseInfo()
184 }
185 return d.getFreeBSDInfo()
186 }
187
188 // Try to parse /etc/os-release first
189 osReleaseInfo, err := d.parseOSRelease()
190 if err != nil {
191 d.logger.WithError(err).Warn("Failed to parse /etc/os-release, falling back to gopsutil")
192
193 // Fallback to gopsutil
194 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
195 defer cancel()
196
197 info, err := host.InfoWithContext(ctx)
198 if err != nil {
199 d.logger.WithError(err).Warn("Failed to get host info")
200 return "", "", err
201 }
202
203 osType = info.Platform
204 osVersion = info.PlatformVersion
205
206 return osType, osVersion, nil
207 }
208
209 // Use NAME for OS type (e.g., "Pop!_OS", "Debian GNU/Linux", "Rocky Linux")
210 osType = osReleaseInfo.Name
211 if osType == "" {
212 osType = "Unknown"
213 }
214
215 // Use VERSION for OS version (e.g., "22.04 LTS", "12 (bookworm)", "10.0 (Red Quartz)")
216 osVersion = osReleaseInfo.Version
217 if osVersion == "" {
218 osVersion = "Unknown"
219 }
220
221 d.logger.WithFields(logrus.Fields{
222 "name": osReleaseInfo.Name,

Callers 2

showDiagnosticsFunction · 0.80
sendReportFunction · 0.80

Calls 5

isFreeBSDMethod · 0.95
isPfSenseMethod · 0.95
getPfSenseInfoMethod · 0.95
getFreeBSDInfoMethod · 0.95
parseOSReleaseMethod · 0.95

Tested by

no test coverage detected