util.go 22 KB

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