github.com/google/cadvisor@v0.49.1/machine/operatingsystem_unix.go (about) 1 // Copyright 2020 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 //go:build freebsd || darwin || linux 16 // +build freebsd darwin linux 17 18 package machine 19 20 import ( 21 "fmt" 22 "os" 23 "os/exec" 24 "regexp" 25 "runtime" 26 "strings" 27 ) 28 29 var rex = regexp.MustCompile("(PRETTY_NAME)=(.*)") 30 31 // getOperatingSystem gets the name of the current operating system. 32 func getOperatingSystem() (string, error) { 33 if runtime.GOOS == "darwin" || runtime.GOOS == "freebsd" { 34 cmd := exec.Command("uname", "-s") 35 osName, err := cmd.Output() 36 if err != nil { 37 return "", err 38 } 39 return string(osName), nil 40 } 41 bytes, err := os.ReadFile("/etc/os-release") 42 if err != nil && os.IsNotExist(err) { 43 // /usr/lib/os-release in stateless systems like Clear Linux 44 bytes, err = os.ReadFile("/usr/lib/os-release") 45 } 46 if err != nil { 47 return "", fmt.Errorf("error opening file : %v", err) 48 } 49 line := rex.FindAllStringSubmatch(string(bytes), -1) 50 if len(line) > 0 { 51 return strings.Trim(line[0][2], "\""), nil 52 } 53 return "Linux", nil 54 }