sysutils.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // Copyright 2019 Yunion
  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. package sysutils
  15. import (
  16. "fmt"
  17. "net"
  18. "os"
  19. "os/exec"
  20. "strconv"
  21. "strings"
  22. "yunion.io/x/jsonutils"
  23. "yunion.io/x/log"
  24. "yunion.io/x/onecloud/pkg/apis/compute"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/types"
  26. "yunion.io/x/onecloud/pkg/compute/baremetal"
  27. )
  28. func valueOfKeyword(line string, key string) *string {
  29. lo := strings.ToLower(line)
  30. ko := strings.ToLower(key)
  31. pos := strings.Index(lo, ko)
  32. if pos >= 0 {
  33. val := strings.TrimSpace(line[pos+len(key):])
  34. return &val
  35. }
  36. return nil
  37. }
  38. func DumpMapToObject(data map[string]string, obj interface{}) error {
  39. return jsonutils.Marshal(data).Unmarshal(obj)
  40. }
  41. func ParseDMISysinfo(lines []string) (*types.SSystemInfo, error) {
  42. if len(lines) == 0 {
  43. return nil, fmt.Errorf("Empty input")
  44. }
  45. keys := map[string]string{
  46. "manufacture": "Manufacturer:",
  47. "model": "Product Name:",
  48. "version": "Version:",
  49. "sn": "Serial Number:",
  50. }
  51. ret := make(map[string]string)
  52. for _, line := range lines {
  53. for key, keyword := range keys {
  54. val := valueOfKeyword(line, keyword)
  55. if val != nil {
  56. ret[key] = *val
  57. }
  58. }
  59. }
  60. info := types.SSystemInfo{}
  61. err := DumpMapToObject(ret, &info)
  62. if err != nil {
  63. return nil, err
  64. }
  65. if strings.ToLower(info.Version) == "none" {
  66. info.Version = ""
  67. }
  68. info.OemName = types.ManufactureOemName(info.Manufacture)
  69. return &info, nil
  70. }
  71. func ParseCPUInfo(lines []string) (*types.SCPUInfo, error) {
  72. cnt := 0
  73. var (
  74. model string
  75. freq string
  76. cache string
  77. microcode string
  78. part string
  79. revision string
  80. )
  81. lv := func(line string) string {
  82. return strings.TrimSpace(line[strings.Index(line, ":")+1:])
  83. }
  84. for _, line := range lines {
  85. if len(model) == 0 && strings.HasPrefix(line, "model name") {
  86. model = lv(line)
  87. }
  88. if len(freq) == 0 && strings.HasPrefix(line, "cpu MHz") {
  89. freq = lv(line)
  90. }
  91. if len(cache) == 0 && strings.HasPrefix(line, "cache size") {
  92. cache = strings.TrimSpace(line[strings.Index(line, ":")+1 : strings.Index(line, " KB")])
  93. }
  94. if len(microcode) == 0 && strings.HasPrefix(line, "microcode") {
  95. microcode = lv(line)
  96. }
  97. if len(part) == 0 && strings.HasPrefix(line, "CPU part") {
  98. part = lv(line)
  99. }
  100. if len(revision) == 0 && strings.HasPrefix(line, "CPU revision") {
  101. revision = lv(line)
  102. }
  103. if strings.HasPrefix(line, "processor") {
  104. cnt += 1
  105. }
  106. }
  107. if len(model) == 0 {
  108. model = part
  109. }
  110. if len(model) == 0 {
  111. log.Errorf("Failed to get cpu model")
  112. }
  113. if len(freq) == 0 {
  114. log.Errorf("Failed to get cpu MHz")
  115. }
  116. if len(cache) == 0 {
  117. log.Errorf("Failed to get cpu cache size")
  118. }
  119. if len(microcode) == 0 {
  120. microcode = revision
  121. }
  122. model = strings.TrimSpace(model)
  123. info := &types.SCPUInfo{
  124. Count: cnt,
  125. Model: model,
  126. Microcode: microcode,
  127. }
  128. if len(cache) > 0 {
  129. info.Cache, _ = strconv.Atoi(cache)
  130. }
  131. if len(freq) > 0 {
  132. freqF, _ := strconv.ParseFloat(freq, 32)
  133. info.Freq = int(freqF)
  134. }
  135. return info, nil
  136. }
  137. func ParseDMICPUInfo(lines []string) *types.SDMICPUInfo {
  138. cnt := 0
  139. for _, line := range lines {
  140. if strings.HasPrefix(line, "Processor Information") {
  141. cnt += 1
  142. }
  143. }
  144. return &types.SDMICPUInfo{
  145. Nodes: cnt,
  146. }
  147. }
  148. func ParseDMIMemInfo(lines []string) *types.SDMIMemInfo {
  149. size := 0
  150. for _, line := range lines {
  151. val := valueOfKeyword(line, "Size:")
  152. if val == nil {
  153. continue
  154. }
  155. // skip 'Volatile Size:' line
  156. if strings.Contains(line, "Volatile") {
  157. continue
  158. }
  159. value := strings.ToLower(*val)
  160. if strings.HasSuffix(value, " mb") {
  161. sizeMb, err := strconv.Atoi(strings.TrimSuffix(value, " mb"))
  162. if err != nil {
  163. log.Errorf("parse MB error: %v", err)
  164. continue
  165. }
  166. size += sizeMb
  167. } else if strings.HasSuffix(value, " gb") {
  168. sizeGb, err := strconv.Atoi(strings.TrimSuffix(value, " gb"))
  169. if err != nil {
  170. log.Errorf("parse GB error: %v", err)
  171. continue
  172. }
  173. size += sizeGb * 1024
  174. }
  175. }
  176. return &types.SDMIMemInfo{Total: size}
  177. }
  178. func ParseDMIIPMIInfo(lines []string) bool {
  179. for _, line := range lines {
  180. val := valueOfKeyword(line, "Interface Type:")
  181. if val != nil {
  182. return true
  183. }
  184. }
  185. return false
  186. }
  187. func ParseNicInfo(lines []string) []*types.SNicDevInfo {
  188. ret := make([]*types.SNicDevInfo, 0)
  189. for _, line := range lines {
  190. dat := strings.Split(line, " ")
  191. if len(dat) > 4 {
  192. dev := dat[0]
  193. mac, _ := net.ParseMAC(dat[1])
  194. speed, _ := strconv.Atoi(dat[2])
  195. up := false
  196. if dat[3] == "1" {
  197. up = true
  198. }
  199. mtu, _ := strconv.Atoi(dat[4])
  200. ret = append(ret, &types.SNicDevInfo{
  201. Dev: dev,
  202. Mac: mac,
  203. Speed: speed,
  204. Up: &up,
  205. Mtu: mtu,
  206. })
  207. }
  208. }
  209. return ret
  210. }
  211. func ParseDiskInfo(lines []string, driver string) []*types.SDiskInfo {
  212. ret := make([]*types.SDiskInfo, 0)
  213. for _, line := range lines {
  214. data := strings.Split(line, " ")
  215. if len(data) <= 6 {
  216. continue
  217. }
  218. dev := data[0]
  219. sector, _ := strconv.Atoi(data[1])
  220. size := sector * 512 / 1024 / 1024
  221. block, _ := strconv.Atoi(data[2])
  222. rotate := false
  223. if data[3] == "1" {
  224. rotate = true
  225. }
  226. kernel := data[4]
  227. pciCls := data[5]
  228. modinfo := strings.Join(data[6:], " ")
  229. ret = append(ret, &types.SDiskInfo{
  230. Dev: dev,
  231. Sector: int64(sector),
  232. Block: int64(block),
  233. Size: int64(size),
  234. Rotate: rotate,
  235. ModuleInfo: modinfo,
  236. Kernel: kernel,
  237. PCIClass: pciCls,
  238. Driver: driver,
  239. })
  240. }
  241. return ret
  242. }
  243. func ParsePCIEDiskInfo(lines []string) []*types.SDiskInfo {
  244. return ParseDiskInfo(lines, baremetal.DISK_DRIVER_PCIE)
  245. }
  246. func ParseSCSIDiskInfo(lines []string) []*types.SDiskInfo {
  247. return ParseDiskInfo(lines, baremetal.DISK_DRIVER_LINUX)
  248. }
  249. func GetSecureTTYs(lines []string) []string {
  250. ttys := []string{}
  251. for _, l := range lines {
  252. if len(l) == 0 {
  253. continue
  254. }
  255. if strings.HasPrefix(l, "#") {
  256. continue
  257. }
  258. ttys = append(ttys, l)
  259. }
  260. return ttys
  261. }
  262. func GetSerialPorts(lines []string) []string {
  263. // http://wiki.networksecuritytoolkit.org/index.php/Console_Output_and_Serial_Terminals
  264. ret := []string{}
  265. for _, l := range lines {
  266. if strings.Contains(l, "CTS") || strings.Contains(l, "RTS") {
  267. pos := strings.Index(l, ":")
  268. if pos < 0 {
  269. continue
  270. }
  271. idx := l[0:pos]
  272. ret = append(ret, fmt.Sprintf("ttyS%s", idx))
  273. }
  274. }
  275. return ret
  276. }
  277. // ParseSGMap parse command 'sg_map -x' outputs:
  278. //
  279. // /dev/sg1 0 2 0 0 0 /dev/sda
  280. // /dev/sg2 0 2 1 0 0 /dev/sdb
  281. // /dev/sg3 0 2 2 0 0 /dev/sdc
  282. func ParseSGMap(lines []string) []compute.SGMapItem {
  283. ret := make([]compute.SGMapItem, 0)
  284. for _, l := range lines {
  285. items := strings.Fields(l)
  286. if len(items) != 7 {
  287. continue
  288. }
  289. hostNum, _ := strconv.Atoi(items[1])
  290. bus, _ := strconv.Atoi(items[2])
  291. scsiId, _ := strconv.Atoi(items[3])
  292. lun, _ := strconv.Atoi(items[4])
  293. typ, _ := strconv.Atoi(items[5])
  294. ret = append(ret, compute.SGMapItem{
  295. SGDeviceName: items[0],
  296. HostNumber: hostNum,
  297. Bus: bus,
  298. SCSIId: scsiId,
  299. Lun: lun,
  300. Type: typ,
  301. LinuxDeviceName: items[6],
  302. })
  303. }
  304. return ret
  305. }
  306. func Start(closeFd bool, args ...string) (p *os.Process, err error) {
  307. if args[0], err = exec.LookPath(args[0]); err == nil {
  308. var procAttr os.ProcAttr
  309. if closeFd {
  310. procAttr.Files = []*os.File{nil, nil, nil}
  311. } else {
  312. procAttr.Files = []*os.File{os.Stdin,
  313. os.Stdout, os.Stderr}
  314. }
  315. p, err := os.StartProcess(args[0], args, &procAttr)
  316. if err == nil {
  317. return p, nil
  318. }
  319. }
  320. return nil, err
  321. }
  322. func ParseIPMIUser(lines []string) []compute.IPMIUser {
  323. ret := make([]compute.IPMIUser, 0)
  324. for _, l := range lines {
  325. if strings.HasPrefix(l, "ID") {
  326. continue
  327. }
  328. fields := strings.Fields(l)
  329. if strings.Contains(l, "Empty User") {
  330. id, err := strconv.Atoi(fields[0])
  331. if err != nil {
  332. continue
  333. }
  334. ret = append(ret, compute.IPMIUser{Id: id})
  335. continue
  336. }
  337. if len(fields) != 6 {
  338. continue
  339. }
  340. id, err := strconv.Atoi(fields[0])
  341. if err != nil {
  342. continue
  343. }
  344. ret = append(ret, compute.IPMIUser{
  345. Id: id,
  346. Name: fields[1],
  347. Priv: fields[5],
  348. })
  349. }
  350. return ret
  351. }
  352. const (
  353. UUID_EMPTY = "00000000-0000-0000-0000-000000000000"
  354. )
  355. func NormalizeUuid(uuid string) string {
  356. uuid = strings.ToLower(uuid)
  357. if uuid == UUID_EMPTY {
  358. uuid = ""
  359. }
  360. return uuid
  361. }