util.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. package utils
  2. import (
  3. "archive/tar"
  4. "archive/zip"
  5. "bufio"
  6. "bytes"
  7. "compress/gzip"
  8. "crypto/md5"
  9. "encoding/base64"
  10. "encoding/json"
  11. "errors"
  12. "fmt"
  13. "github.com/coroot/coroot-node-agent/logs"
  14. "github.com/coroot/coroot-node-agent/utils/enums"
  15. "io"
  16. "io/ioutil"
  17. "net/http"
  18. "os"
  19. "os/exec"
  20. "path/filepath"
  21. "regexp"
  22. "runtime"
  23. "runtime/debug"
  24. "strconv"
  25. "strings"
  26. . "strings"
  27. "sync"
  28. "time"
  29. log "github.com/sirupsen/logrus"
  30. )
  31. var (
  32. agentdaemon_home = ""
  33. AgentsPath = "agents"
  34. BinPath = "bin"
  35. ConfPath = "conf"
  36. LogsPath = "logs"
  37. ConfName = "daemon.conf"
  38. ComIniFile = "common.ini"
  39. LogName = "daemon.log"
  40. ScriptPath = "scripts"
  41. RuntimePath = "runtime"
  42. MetaPath = "meta"
  43. )
  44. func init() {
  45. logs.FormatterInit()
  46. }
  47. func init() {
  48. // TODO: 后面校验所有的路径,都要以agentdaemon_home开头.... @jay
  49. // 初始化时获取omniagent的工作目录
  50. if ex, err := os.Executable(); err != nil {
  51. agentdaemon_home = ""
  52. // TODO: 不应该exit吗?@jay
  53. } else {
  54. agentdaemon_home = ex
  55. }
  56. tmp_dir, _ := filepath.EvalSymlinks(os.TempDir())
  57. if tmp_dir != "" && strings.Contains(agentdaemon_home, tmp_dir) {
  58. _, filename, _, ok := runtime.Caller(0)
  59. if ok {
  60. agentdaemon_home = filepath.Dir(filename)
  61. }
  62. }
  63. agentdaemon_home = filepath.Dir(filepath.Dir(agentdaemon_home))
  64. log.Infof("omniagent work path %s", agentdaemon_home)
  65. _, filename := filepath.Split(agentdaemon_home)
  66. if !strings.EqualFold(filename, "omniagent") && !strings.EqualFold(filename, "doccagent") && !strings.EqualFold(filename, "cloudwise") {
  67. // 考虑后续支持自定义目录安装的情况 卸载逻辑在uninstall.sh中判断
  68. //panic(errors.New(fmt.Sprintf("get exec path[%s] is not doccagent work path panic error", agentdaemon_home)))
  69. }
  70. log.Infof("omniagent exec path %s", agentdaemon_home)
  71. //initCntrPidConvert()
  72. }
  73. func MD5(src string) (dest string) {
  74. dest = fmt.Sprintf("%x", md5.Sum([]byte(src)))
  75. return dest
  76. }
  77. // 检查url是否正确
  78. func CheckUrl(url string) error {
  79. if url == "" {
  80. err := errors.New("url is empty")
  81. return err
  82. }
  83. // 对接软负载后直接返回uri即可
  84. //if !strings.Contains(url, "http") {
  85. // err := errors.New("URL is incorrect,url is " + url)
  86. // return err
  87. //}
  88. return nil
  89. }
  90. // DecompressTarGzip 将 TAR.GZ 格式的文件解包到指定目录并返回指定文件所在的解包后的目录,
  91. // 如果存在多个同名指定文件,则返回第一个找到的目录
  92. func decompress(srcPath, dstPath string) (targetDir string, _ error) {
  93. linkMap := make(map[string]string)
  94. if err := os.MkdirAll(dstPath, os.ModePerm); err != nil {
  95. return "", err
  96. }
  97. if strings.HasSuffix(srcPath, ".zip") {
  98. zr, err := zip.OpenReader(srcPath)
  99. if err != nil {
  100. return "", err
  101. }
  102. defer zr.Close()
  103. for _, k := range zr.File {
  104. info := k.FileInfo()
  105. fpath := filepath.Join(dstPath, k.Name)
  106. if k.FileInfo().IsDir() {
  107. err := os.MkdirAll(fpath, info.Mode())
  108. if err != nil {
  109. return "", err
  110. }
  111. continue
  112. }
  113. w, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, info.Mode())
  114. if err != nil {
  115. return "", err
  116. }
  117. r, err := k.Open()
  118. if err != nil {
  119. return "", err
  120. }
  121. io.Copy(w, r)
  122. w.Close()
  123. }
  124. return dstPath, nil
  125. }
  126. srcFile, err := os.Open(srcPath)
  127. if err != nil {
  128. return "", err
  129. }
  130. defer srcFile.Close()
  131. gr, err := gzip.NewReader(srcFile)
  132. if err != nil {
  133. return "", err
  134. }
  135. defer gr.Close()
  136. tr := tar.NewReader(gr)
  137. count := 1
  138. for {
  139. header, err := tr.Next()
  140. if err == io.EOF {
  141. break
  142. } else if err != nil {
  143. return "", err
  144. }
  145. if strings.HasPrefix(header.Name, ".") {
  146. continue
  147. }
  148. if header.Linkname != "" {
  149. hnd := filepath.Dir(header.Name)
  150. hld := filepath.Join(hnd, header.Linkname)
  151. linkMap[filepath.Join(dstPath, hld)] = filepath.Join(dstPath, header.Name)
  152. continue
  153. }
  154. fpath := filepath.Join(dstPath, header.Name)
  155. if count == 1 {
  156. fmt.Println(fpath, header.Name)
  157. targetDir = fpath
  158. }
  159. count++
  160. info := header.FileInfo()
  161. if info.IsDir() {
  162. if err = os.MkdirAll(fpath, info.Mode()); err != nil {
  163. return "", err
  164. }
  165. continue
  166. }
  167. if err = extractTarFileToPath(tr, info, fpath); err != nil {
  168. return "", err
  169. }
  170. }
  171. linkMapHandle(linkMap)
  172. linkMap = nil
  173. return targetDir, nil
  174. }
  175. func decompressWithTar(srcPath, dstPath string) (targetDir string, _ error) {
  176. if err := os.MkdirAll(dstPath, os.ModePerm); err != nil {
  177. return "", err
  178. }
  179. if strings.HasSuffix(srcPath, ".zip") {
  180. return "", errors.New("unsupported zip file")
  181. }
  182. compressPath, err := makeCompressFolderAndMvTarFile2(srcPath)
  183. if err != nil {
  184. return "", err
  185. }
  186. folderName, err := decompressCjeInCurrentDirectory(compressPath)
  187. if err != nil {
  188. os.Rename(compressPath, srcPath)
  189. return "", err
  190. }
  191. return filepath.Join(filepath.Dir(srcPath), "compress", folderName), nil
  192. }
  193. func makeCompressFolderAndMvTarFile2(path string) (string, error) {
  194. baseName := filepath.Base(path)
  195. folder := filepath.Dir(path)
  196. compressPath := filepath.Join(folder, "compress")
  197. if _, err := os.Stat(compressPath); os.IsNotExist(err) {
  198. os.Mkdir(compressPath, os.ModePerm)
  199. }
  200. moveToPath := filepath.Join(compressPath, baseName)
  201. if err := os.Rename(path, moveToPath); err != nil {
  202. return "", err
  203. }
  204. return moveToPath, nil
  205. }
  206. func decompressCjeInCurrentDirectory(gzipPath string) (string, error) {
  207. folder := filepath.Dir(gzipPath)
  208. dir, err := ioutil.ReadDir(folder)
  209. for _, d := range dir {
  210. if d.Name() == filepath.Base(gzipPath) {
  211. continue
  212. } else {
  213. err = RemovePath(filepath.Join(folder, d.Name()), "")
  214. if err != nil {
  215. log.Errorf("remove dir error %s", err)
  216. }
  217. }
  218. }
  219. cmd := exec.Command("tar", "-vzxf", gzipPath)
  220. cmd.Dir = folder
  221. err = cmd.Run()
  222. if err != nil {
  223. return "", err
  224. }
  225. fileInfoList, err := ioutil.ReadDir(folder)
  226. if err != nil {
  227. return "", err
  228. }
  229. for _, file := range fileInfoList {
  230. if file.Name() == filepath.Base(gzipPath) {
  231. continue
  232. }
  233. return file.Name(), nil
  234. }
  235. return "", fmt.Errorf("uncompress faild unknow error")
  236. }
  237. // extractTarFileToPath 写出 *tar.Reader 对象的内容到指定路径
  238. func extractTarFileToPath(tr *tar.Reader, info os.FileInfo, path string) error {
  239. w, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, info.Mode())
  240. if err != nil {
  241. return err
  242. }
  243. defer w.Close()
  244. _, err = io.Copy(w, tr)
  245. if err != nil {
  246. return err
  247. }
  248. return nil
  249. }
  250. func linkMapHandle(m map[string]string) {
  251. for s, s2 := range m {
  252. err := os.Link(s, s2)
  253. if err != nil {
  254. log.Errorf("linkMapHandle err %s => %s, err is %v", s, s2, err)
  255. }
  256. }
  257. }
  258. var CPUNum = float64(runtime.NumCPU())
  259. func FormatPath(s string) string {
  260. switch runtime.GOOS {
  261. case "windows":
  262. return Replace(s, "/", "\\", -1)
  263. case "darwin", "linux":
  264. return Replace(s, "\\", "/", -1)
  265. default:
  266. log.Infof("only support linux,windows,darwin, but os is " + runtime.GOOS)
  267. return s
  268. }
  269. }
  270. func CopyDir(src string, dest string) {
  271. src = FormatPath(src)
  272. dest = FormatPath(dest)
  273. var cmd *exec.Cmd
  274. switch runtime.GOOS {
  275. case "windows":
  276. cmd = exec.Command("xcopy", src, dest, "/I", "/E")
  277. case "darwin", "linux":
  278. cmd = exec.Command("cp", "-R", src, dest)
  279. }
  280. outPut, err := cmd.Output()
  281. if err != nil {
  282. log.Infof("copyDir %s => %s ,err is => %v", src, dest, err)
  283. return
  284. }
  285. log.Infof("copyDir %s => %s output is => %s", src, dest, string(outPut))
  286. }
  287. func WriteConf(path string, new map[string]interface{}) error {
  288. info, err := os.Stat(path)
  289. if err != nil {
  290. log.Errorf("write config[%s] get file info error:%s", path, err)
  291. return err
  292. }
  293. file, err := os.OpenFile(path, os.O_WRONLY, info.Mode())
  294. if err != nil {
  295. log.Errorf("write config[%s] open file error:%s", path, err)
  296. return err
  297. }
  298. file.Truncate(0)
  299. defer file.Close()
  300. data, err := json.MarshalIndent(&new, "", "\t")
  301. if err != nil {
  302. log.Errorf("write config[%s] write json data error:%s", path, err)
  303. return err
  304. }
  305. file.Write(data)
  306. return nil
  307. }
  308. func HTTPConnect(method, url, token string, data []byte) ([]byte, error) {
  309. req, err := http.NewRequest(method, url, bytes.NewBuffer(data))
  310. if err != nil {
  311. log.Errorf("create request error:%s.", err)
  312. return nil, err
  313. }
  314. req.Header.Set("Content-Type", "application/json")
  315. req.Header.Add("token", token)
  316. client := &http.Client{
  317. Timeout: 60 * time.Second,
  318. }
  319. resp, err := client.Do(req)
  320. if err != nil {
  321. log.Error(err)
  322. return nil, err
  323. }
  324. defer resp.Body.Close()
  325. result, err := ioutil.ReadAll(resp.Body)
  326. if err != nil {
  327. log.Errorf("http read body error:%s", err)
  328. return nil, err
  329. }
  330. return result, nil
  331. }
  332. func ReadConfig(data []byte) (map[string]interface{}, error) {
  333. result := map[string]interface{}{}
  334. err := json.Unmarshal(data, &result)
  335. if err != nil {
  336. return result, err
  337. }
  338. return result, nil
  339. }
  340. func updateConfig(new map[string]interface{}, old map[string]interface{}) map[string]interface{} {
  341. for k, v := range new {
  342. if value, ok := old[k]; ok {
  343. newconf, newok := v.(map[string]interface{})
  344. oldconf, oldok := value.(map[string]interface{})
  345. if newok && oldok {
  346. new[k] = updateConfig(newconf, oldconf)
  347. continue
  348. }
  349. new[k] = value
  350. }
  351. }
  352. return new
  353. }
  354. func UpdateConfig(oldPath, newPath string) error {
  355. oldConfByte, err := ioutil.ReadFile(oldPath)
  356. if err != nil {
  357. return err
  358. }
  359. oldConf, err := ReadConfig(oldConfByte)
  360. if err != nil {
  361. return err
  362. }
  363. newConfByte, err := ioutil.ReadFile(newPath)
  364. if err != nil {
  365. return err
  366. }
  367. newConf, err := ReadConfig(newConfByte)
  368. if err != nil {
  369. return err
  370. }
  371. new := updateConfig(newConf, oldConf)
  372. err = WriteConf(oldPath, new)
  373. if err != nil {
  374. WriteConf(oldPath, oldConf)
  375. return err
  376. }
  377. return nil
  378. }
  379. func CopyFile(dstName, srcName string) (written int64, err error) {
  380. src, err := os.Open(srcName)
  381. if err != nil {
  382. return
  383. }
  384. defer src.Close()
  385. dst, err := os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE, 0777)
  386. if err != nil {
  387. return
  388. }
  389. defer dst.Close()
  390. return io.Copy(dst, src)
  391. }
  392. func CheckPath(path string) (err error) {
  393. path, err = filepath.Abs(path)
  394. if err != nil {
  395. log.Errorf("check path[%s] get abs path error %s", path, err)
  396. return err
  397. }
  398. // 通过比较路径的长短来判断是否为子路径
  399. if len(path) < len(agentdaemon_home) {
  400. log.Errorf("remove path[%s] check path length error", path)
  401. return errors.New(fmt.Sprintf("remove path[%s] check path length error", path))
  402. }
  403. // 通过比较父路径是否相同来判断是否为子路径
  404. if agentdaemon_home != path[:len(agentdaemon_home)] {
  405. log.Errorf("remove path[%s] check path is not subdirectory[%s]", path, agentdaemon_home)
  406. return errors.New(fmt.Sprintf("remove path[%s] check path is not subdirectory[%s]", path, agentdaemon_home))
  407. }
  408. return nil
  409. }
  410. // 校验删除的目录是否为doccagent工作目录的子目录
  411. func RemovePath(path, filename string) (err error) {
  412. // 校验删除的文件名
  413. _, real_filename := filepath.Split(path)
  414. if filename != "" && real_filename != filename {
  415. log.Errorf("remove path[%s] check filename is not %s", path, filename)
  416. return errors.New(fmt.Sprintf("remove path[%s] check filename is not %s", path, filename))
  417. }
  418. stat, err := os.Stat(path)
  419. if os.IsNotExist(err) {
  420. return nil
  421. }
  422. if err != nil {
  423. log.Errorf("remove path[%s] get file stat error %s", path, err)
  424. return err
  425. }
  426. if stat.IsDir() {
  427. err = os.RemoveAll(path)
  428. } else {
  429. err = os.Remove(path)
  430. }
  431. if err != nil {
  432. log.Errorf("remove path[%s] error %s", path, err)
  433. }
  434. return err
  435. }
  436. func GetRootPath() string {
  437. return agentdaemon_home
  438. }
  439. func GetDefaultPath(path_name ...string) string {
  440. return filepath.Join(agentdaemon_home, filepath.Join(path_name...))
  441. }
  442. func GetDefaultConfigPath() string {
  443. return GetDefaultPath(ConfPath, ConfName)
  444. }
  445. func GetDefaultLogPath() string {
  446. return GetDefaultPath(LogsPath)
  447. }
  448. func GetDefaultBasePath() string {
  449. return filepath.Join(GetDefaultPath(ScriptPath), "base")
  450. }
  451. func GetControlProc() string {
  452. return filepath.Join(GetDefaultPath(ScriptPath), enums.DaemonCtlProc)
  453. }
  454. func GetCommonIni() string {
  455. return GetDefaultPath(ConfPath, ComIniFile)
  456. }
  457. func GetPluginInstallParamsPath(pluginId string) string {
  458. if pluginId != "" {
  459. pluginId += ".conf"
  460. }
  461. return GetDefaultPath(ConfPath, "agents-installation", pluginId)
  462. }
  463. func CatchFn(err interface{}) {
  464. log.Errorf(fmt.Sprintf("panic : %s\n", err))
  465. log.Errorf(fmt.Sprint(string(debug.Stack())))
  466. }
  467. func FileExist(path string) bool {
  468. _, err := os.Stat(path) //os.Stat获取文件信息
  469. if os.IsNotExist(err) {
  470. return false
  471. }
  472. return true
  473. }
  474. /**
  475. * 从token中解析accountid/userid
  476. */
  477. func DecodeAccountUser(token string) (string, string) {
  478. var account_id string
  479. var user_id string
  480. account_and_user_byte, err := base64.StdEncoding.DecodeString(token)
  481. if err != nil {
  482. return account_id, user_id
  483. }
  484. account_and_user_string := strings.Split(string(account_and_user_byte), "@")
  485. if len(account_and_user_string) > 0 {
  486. account_id = account_and_user_string[0]
  487. }
  488. return account_id, user_id
  489. }
  490. /**
  491. * 读取原配置项
  492. */
  493. func GetOriginConfig() (map[string]interface{}, error) {
  494. configMap := make(map[string]interface{})
  495. content, err := ioutil.ReadFile(GetDefaultConfigPath())
  496. if err != nil {
  497. return nil, err
  498. }
  499. err = json.Unmarshal(content, &configMap)
  500. return configMap, err
  501. }
  502. func Capitalize(str string) string {
  503. if len(str) == 0 {
  504. return str
  505. }
  506. upperStr := strings.ToUpper(string(str[0]))
  507. if len(str) > 1 {
  508. return upperStr + str[1:]
  509. }
  510. return upperStr
  511. }
  512. func CreatePidFile() error {
  513. pid := os.Getpid()
  514. pidFile := GetDefaultPath(RuntimePath, enums.DaemonProc+".pid")
  515. // 如果pid文件存在,覆盖写入
  516. if !FileExist(pidFile) {
  517. _, err := os.Create(pidFile)
  518. if err != nil {
  519. return err
  520. }
  521. }
  522. err := ioutil.WriteFile(pidFile, []byte(strconv.Itoa(pid)), 0644)
  523. return err
  524. }
  525. // back up file
  526. func BackUpFile(src string, dst string, list []string) error {
  527. // backup file
  528. backupDir := filepath.Join(dst, "backup")
  529. for _, v := range list {
  530. srcPath := filepath.Join(src, v)
  531. dstPath := filepath.Join(backupDir, v)
  532. // rename file
  533. err := os.Rename(srcPath, dstPath)
  534. if err != nil {
  535. // log.Errorf("backup file %s err : %v", v, err)
  536. return err
  537. }
  538. }
  539. return nil
  540. }
  541. func CopyFiles(srcDir string, dstDir string, list []string) error {
  542. // if srcDir not exist ,return
  543. if _, err := os.Stat(srcDir); os.IsNotExist(err) {
  544. log.Debugf("srcDir is not exist %s err : %v", srcDir, err)
  545. return err
  546. }
  547. // if dstdir not exist ,create it
  548. if _, err := os.Stat(dstDir); os.IsNotExist(err) {
  549. err := os.MkdirAll(dstDir, os.ModePerm)
  550. if err != nil {
  551. log.Errorf("mkdir dst dir %s err : %v", dstDir, err)
  552. return err
  553. }
  554. }
  555. // Copy list dir or file to dstDir
  556. for _, v := range list {
  557. srcPath := filepath.Join(srcDir, v)
  558. dstPath := filepath.Join(dstDir, v)
  559. // src is dir
  560. fsInfo, err := os.Stat(srcPath)
  561. if err != nil {
  562. log.Debugf("srcPath is %s, err is %v", srcPath, err)
  563. continue
  564. }
  565. // log err
  566. log.Debugf("srcPath is %s, err is %v", srcPath, err)
  567. if fsInfo.IsDir() {
  568. // mkdir dst dir
  569. err := os.MkdirAll(filepath.Dir(dstPath), os.ModePerm)
  570. if err != nil {
  571. log.Errorf("mkdir dst dir %s err : %v", dstPath, err)
  572. return err
  573. }
  574. CopyDir(srcPath, dstPath)
  575. } else {
  576. // copy file
  577. err = os.MkdirAll(filepath.Dir(dstPath), os.ModePerm)
  578. if err != nil {
  579. log.Errorf("mkdir dst dir %s err : %v", dstPath, err)
  580. return err
  581. }
  582. _, err := CopyFile(dstPath, srcPath)
  583. if err != nil {
  584. log.Errorf("copy file %s err : %v", v, err)
  585. return err
  586. }
  587. }
  588. }
  589. return nil
  590. }
  591. // // CopyDir 拷贝整个目录
  592. // func CopyDir(src string, dst string, overwrite bool) error {
  593. // // 创建目标目录
  594. // err := os.MkdirAll(dst, 0755)
  595. // if err != nil {
  596. // return err
  597. // }
  598. // // 打开源目录
  599. // entries, err := os.ReadDir(src)
  600. // if err != nil {
  601. // return err
  602. // }
  603. // // 遍历源目录中的条目
  604. // for _, entry := range entries {
  605. // sourcePath := filepath.Join(src, entry.Name())
  606. // destinationPath := filepath.Join(dst, entry.Name())
  607. // // 如果是目录,则递归拷贝
  608. // if entry.IsDir() {
  609. // err = CopyDir(sourcePath, destinationPath, overwrite)
  610. // if err != nil {
  611. // return err
  612. // }
  613. // } else {
  614. // // 如果是文件,则拷贝文件
  615. // err = CopyFile(sourcePath, destinationPath, overwrite)
  616. // if err != nil {
  617. // return err
  618. // }
  619. // }
  620. // }
  621. // return nil
  622. // }
  623. func NewInstallPathParams(installPath string) []string {
  624. // check dir is prefix of "/D="
  625. if !strings.HasPrefix(installPath, "/D=") {
  626. installPath = "/D=" + installPath
  627. }
  628. paths := strings.Split(installPath, " ")
  629. return paths
  630. }
  631. func GetHostPid(pid int) (int, error) {
  632. if os.Getenv("CW_CONTAINER") == "true" {
  633. if cntrPidConvert != nil {
  634. hostPid, err := cntrPidConvert.getHostPidByHostProc(pid)
  635. if err != nil {
  636. log.Errorf("getHostPidByHostProc %d error:%v", pid, err)
  637. } else {
  638. log.Infof("getHostPidByHostProc %d hostPid:%d", pid, hostPid)
  639. }
  640. return hostPid, err
  641. }
  642. hostPid, err := getHostPidBySched(pid)
  643. if err != nil {
  644. log.Errorf("getHostPidBySched %d error:%v", pid, err)
  645. } else {
  646. log.Infof("getHostPidBySched %d hostPid:%d", pid, hostPid)
  647. }
  648. return hostPid, err
  649. } else {
  650. return pid, nil
  651. }
  652. }
  653. func getHostPidBySched(pid int) (int, error) {
  654. hostPid := 0
  655. schedFile := fmt.Sprintf("/proc/%d/sched", pid)
  656. file, err := os.Open(schedFile)
  657. if err != nil {
  658. return 0, err
  659. }
  660. defer file.Close()
  661. scanner := bufio.NewScanner(file)
  662. if scanner.Scan() {
  663. line := scanner.Text()
  664. re := regexp.MustCompile(`\d+`)
  665. match := re.FindString(line)
  666. if match != "" {
  667. hostPid, err = strconv.Atoi(match)
  668. if err != nil {
  669. return 0, err
  670. }
  671. }
  672. }
  673. return hostPid, nil
  674. }
  675. type cntrPidConvertMgr struct {
  676. cntrPidToHostPid map[int]int
  677. mtxCntrPidConvert *sync.RWMutex
  678. daemonid []byte
  679. }
  680. var cntrPidConvert *cntrPidConvertMgr
  681. func initCntrPidConvert() {
  682. if os.Getenv("CW_CONTAINER") == "true" && os.Getenv("CW_HOST_PID_ENABLE") != "true" {
  683. if hostPid, err := getHostPidBySched(1); err == nil && hostPid > 1 {
  684. log.Infof("not need convert cntrPid to hostPid")
  685. return
  686. }
  687. host_root := os.Getenv("HOST_ROOT")
  688. host_proc := os.Getenv("HOST_PROC")
  689. log.Infof("host_root %s, host_proc %s", host_root, host_proc)
  690. if len(host_proc) <= len(host_root) || host_proc[:len(host_root)] != host_root {
  691. log.Errorf("HOST_ROOT %s is not prefix of HOST_PROC %s", host_root, host_proc)
  692. return
  693. }
  694. //若host_proc目录不存在,直接返回
  695. if _, err := os.Stat(host_proc); os.IsNotExist(err) {
  696. log.Errorf("host_proc %s not exist", host_proc)
  697. return
  698. }
  699. daemonidPath := filepath.Join(GetDefaultPath(MetaPath, ".daemonid"))
  700. daemonid, err := ioutil.ReadFile(daemonidPath)
  701. if len(daemonid) == 0 {
  702. log.Errorf("read daemonid from %s fail: %v", daemonidPath, err)
  703. return
  704. }
  705. log.Infof("daemonid %s", daemonid)
  706. cntrPidConvert = &cntrPidConvertMgr{
  707. cntrPidToHostPid: make(map[int]int),
  708. mtxCntrPidConvert: &sync.RWMutex{},
  709. daemonid: daemonid,
  710. }
  711. }
  712. }
  713. func (c *cntrPidConvertMgr) getHostPidByHostProc(pid int) (int, error) {
  714. c.mtxCntrPidConvert.RLock()
  715. hostPid, ok := c.cntrPidToHostPid[pid]
  716. c.mtxCntrPidConvert.RUnlock()
  717. if ok {
  718. return hostPid, nil
  719. }
  720. var err error
  721. c.mtxCntrPidConvert.Lock()
  722. defer c.mtxCntrPidConvert.Unlock()
  723. hostPid, ok = c.cntrPidToHostPid[pid]
  724. if ok {
  725. return hostPid, nil
  726. }
  727. timeStart := time.Now()
  728. c.cntrPidToHostPid, err = parseProcDirectory(os.Getenv("OS_HOST_PROC"), c.daemonid)
  729. log.Infof("cntrPidToHostPid %#v, time_%s: %#v", c.cntrPidToHostPid, time.Since(timeStart).String(), err)
  730. if err != nil {
  731. return 0, err
  732. }
  733. hostPid, ok = c.cntrPidToHostPid[pid]
  734. if ok {
  735. return hostPid, nil
  736. } else {
  737. return 0, fmt.Errorf("can not find pid %d in container", pid)
  738. }
  739. }
  740. func parseProcDirectory(dirPath string, daemonid []byte) (pidMap map[int]int, err error) {
  741. pidMap = make(map[int]int)
  742. daemonidPath := filepath.Join(GetDefaultPath(MetaPath, ".daemonid"))
  743. fileInfo, err := os.Stat(daemonidPath)
  744. if err != nil {
  745. return
  746. }
  747. daemonidFileSize := fileInfo.Size()
  748. // 遍历指定目录下的PID目录
  749. files, err := ioutil.ReadDir(dirPath)
  750. if err != nil {
  751. return
  752. }
  753. for _, file := range files {
  754. if file.IsDir() {
  755. pid := file.Name()
  756. pidInt, err := strconv.Atoi(pid)
  757. if err != nil {
  758. continue
  759. }
  760. daemonidFilePath := filepath.Join(dirPath, pid, "root/opt/cloudwise/omniagent/meta/.daemonid")
  761. // 检查是否存在指定文件并size等于指定大小
  762. fileInfo, err = os.Stat(daemonidFilePath)
  763. if err != nil {
  764. continue
  765. }
  766. fileSize := fileInfo.Size()
  767. if fileSize != daemonidFileSize {
  768. log.Errorf("%s file size %d not equal %d", daemonidFilePath, fileSize, daemonidFileSize)
  769. continue
  770. }
  771. // 检查是否存在指定文件并内容等于指定内容
  772. daemonidContentBytes, err := ioutil.ReadFile(daemonidFilePath)
  773. if err == nil && bytes.Compare(daemonidContentBytes, daemonid) == 0 {
  774. statusFilePath := filepath.Join(dirPath, pid, "status")
  775. // 解析status文件获取NSpid的第二个字符串
  776. nspidValue, err := getNSpidValue(statusFilePath)
  777. if err == nil {
  778. pidMap[nspidValue] = pidInt
  779. } else {
  780. log.Errorf("getNSpidValue %s error:%v", statusFilePath, err)
  781. }
  782. }
  783. }
  784. }
  785. return pidMap, nil
  786. }
  787. func getNSpidValue(filePath string) (int, error) {
  788. content, err := ioutil.ReadFile(filePath)
  789. if err != nil {
  790. return 0, err
  791. }
  792. lines := strings.Split(string(content), "\n")
  793. for _, line := range lines {
  794. if strings.HasPrefix(line, "NSpid:") {
  795. fields := strings.Fields(line)
  796. if len(fields) >= 3 {
  797. valueStr := fields[2]
  798. value, err := strconv.Atoi(valueStr)
  799. if err != nil {
  800. return 0, fmt.Errorf("failed to convert NSpid value %s to integer: %v", valueStr, err)
  801. }
  802. return value, nil
  803. } else if len(fields) == 2 {
  804. valueStr := fields[1]
  805. value, err := strconv.Atoi(valueStr)
  806. if err != nil {
  807. return 0, fmt.Errorf("failed to convert NSpid value %s to integer: %v", valueStr, err)
  808. }
  809. return value, nil
  810. } else {
  811. log.Errorf("invalid NSpid line format in %s: %s", filePath, line)
  812. }
  813. }
  814. }
  815. return 0, errors.New("NSpid value not found!")
  816. }