util.go 22 KB

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