util.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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 = "config.yaml"
  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 GetDefaultConfPath() string {
  257. return GetDefaultPath(ConfPath)
  258. }
  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(srcName, dstName 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 GetDefaultRuntimePath() string {
  449. return GetDefaultPath(RuntimePath)
  450. }
  451. func GetDefaultScriptsPath(path_name ...string) string {
  452. return GetDefaultPath(ScriptPath, filepath.Join(path_name...))
  453. }
  454. func GetDefaultLibsPath(path_name ...string) string {
  455. return GetDefaultPath(LibsPath, runtime.GOARCH, filepath.Join(path_name...))
  456. }
  457. func GetDefaultAgentsPath(path_name ...string) string {
  458. return GetDefaultPath(AgentsPath, filepath.Join(path_name...))
  459. }
  460. func GetDefaultBasePath() string {
  461. return filepath.Join(GetDefaultPath(ScriptPath), "base")
  462. }
  463. func GetControlProc() string {
  464. return filepath.Join(GetDefaultPath(ScriptPath), enums.DaemonCtlProc)
  465. }
  466. func GetCommonIni() string {
  467. return GetDefaultPath(ConfPath, ComIniFile)
  468. }
  469. func GetPluginInstallParamsPath(pluginId string) string {
  470. if pluginId != "" {
  471. pluginId += ".conf"
  472. }
  473. return GetDefaultPath(ConfPath, "agents-installation", pluginId)
  474. }
  475. func CatchFn(err interface{}) {
  476. log.Errorf(fmt.Sprintf("panic : %s\n", err))
  477. log.Errorf(fmt.Sprint(string(debug.Stack())))
  478. }
  479. func FileExist(path string) bool {
  480. _, err := os.Stat(path) //os.Stat获取文件信息
  481. if os.IsNotExist(err) {
  482. return false
  483. }
  484. return true
  485. }
  486. /**
  487. * 从token中解析accountid/userid
  488. */
  489. func DecodeAccountUser(token string) (string, string) {
  490. var account_id string
  491. var user_id string
  492. account_and_user_byte, err := base64.StdEncoding.DecodeString(token)
  493. if err != nil {
  494. return account_id, user_id
  495. }
  496. account_and_user_string := strings.Split(string(account_and_user_byte), "@")
  497. if len(account_and_user_string) > 0 {
  498. account_id = account_and_user_string[0]
  499. }
  500. return account_id, user_id
  501. }
  502. /**
  503. * 读取原配置项
  504. */
  505. func GetOriginConfig() (map[string]interface{}, error) {
  506. configMap := make(map[string]interface{})
  507. content, err := ioutil.ReadFile(GetDefaultConfigPath())
  508. if err != nil {
  509. return nil, err
  510. }
  511. err = json.Unmarshal(content, &configMap)
  512. return configMap, err
  513. }
  514. func Capitalize(str string) string {
  515. if len(str) == 0 {
  516. return str
  517. }
  518. upperStr := strings.ToUpper(string(str[0]))
  519. if len(str) > 1 {
  520. return upperStr + str[1:]
  521. }
  522. return upperStr
  523. }
  524. func CreatePidFile() error {
  525. pid := os.Getpid()
  526. pidFile := GetDefaultPath(RuntimePath, enums.DaemonProc+".pid")
  527. // 如果pid文件存在,覆盖写入
  528. if !FileExist(pidFile) {
  529. _, err := os.Create(pidFile)
  530. if err != nil {
  531. return err
  532. }
  533. }
  534. err := ioutil.WriteFile(pidFile, []byte(strconv.Itoa(pid)), 0644)
  535. return err
  536. }
  537. // back up file
  538. func BackUpFile(src string, dst string, list []string) error {
  539. // backup file
  540. backupDir := filepath.Join(dst, "backup")
  541. for _, v := range list {
  542. srcPath := filepath.Join(src, v)
  543. dstPath := filepath.Join(backupDir, v)
  544. // rename file
  545. err := os.Rename(srcPath, dstPath)
  546. if err != nil {
  547. // log.Errorf("backup file %s err : %v", v, err)
  548. return err
  549. }
  550. }
  551. return nil
  552. }
  553. func CopyFiles(srcDir string, dstDir string, list []string) error {
  554. // if srcDir not exist ,return
  555. if _, err := os.Stat(srcDir); os.IsNotExist(err) {
  556. log.Debugf("srcDir is not exist %s err : %v", srcDir, err)
  557. return err
  558. }
  559. // if dstdir not exist ,create it
  560. if _, err := os.Stat(dstDir); os.IsNotExist(err) {
  561. err := os.MkdirAll(dstDir, os.ModePerm)
  562. if err != nil {
  563. log.Errorf("mkdir dst dir %s err : %v", dstDir, err)
  564. return err
  565. }
  566. }
  567. // Copy list dir or file to dstDir
  568. for _, v := range list {
  569. srcPath := filepath.Join(srcDir, v)
  570. dstPath := filepath.Join(dstDir, v)
  571. // src is dir
  572. fsInfo, err := os.Stat(srcPath)
  573. if err != nil {
  574. log.Debugf("srcPath is %s, err is %v", srcPath, err)
  575. continue
  576. }
  577. // log err
  578. log.Debugf("srcPath is %s, err is %v", srcPath, err)
  579. if fsInfo.IsDir() {
  580. // mkdir dst dir
  581. err := os.MkdirAll(filepath.Dir(dstPath), os.ModePerm)
  582. if err != nil {
  583. log.Errorf("mkdir dst dir %s err : %v", dstPath, err)
  584. return err
  585. }
  586. CopyDir(srcPath, dstPath)
  587. } else {
  588. // copy file
  589. err = os.MkdirAll(filepath.Dir(dstPath), os.ModePerm)
  590. if err != nil {
  591. log.Errorf("mkdir dst dir %s err : %v", dstPath, err)
  592. return err
  593. }
  594. _, err := CopyFile(dstPath, srcPath)
  595. if err != nil {
  596. log.Errorf("copy file %s err : %v", v, err)
  597. return err
  598. }
  599. }
  600. }
  601. return nil
  602. }
  603. // // CopyDir 拷贝整个目录
  604. // func CopyDir(src string, dst string, overwrite bool) error {
  605. // // 创建目标目录
  606. // err := os.MkdirAll(dst, 0755)
  607. // if err != nil {
  608. // return err
  609. // }
  610. // // 打开源目录
  611. // entries, err := os.ReadDir(src)
  612. // if err != nil {
  613. // return err
  614. // }
  615. // // 遍历源目录中的条目
  616. // for _, entry := range entries {
  617. // sourcePath := filepath.Join(src, entry.Name())
  618. // destinationPath := filepath.Join(dst, entry.Name())
  619. // // 如果是目录,则递归拷贝
  620. // if entry.IsDir() {
  621. // err = CopyDir(sourcePath, destinationPath, overwrite)
  622. // if err != nil {
  623. // return err
  624. // }
  625. // } else {
  626. // // 如果是文件,则拷贝文件
  627. // err = CopyFile(sourcePath, destinationPath, overwrite)
  628. // if err != nil {
  629. // return err
  630. // }
  631. // }
  632. // }
  633. // return nil
  634. // }
  635. func NewInstallPathParams(installPath string) []string {
  636. // check dir is prefix of "/D="
  637. if !strings.HasPrefix(installPath, "/D=") {
  638. installPath = "/D=" + installPath
  639. }
  640. paths := strings.Split(installPath, " ")
  641. return paths
  642. }
  643. func GetHostPid(pid int) (int, error) {
  644. if os.Getenv("CW_CONTAINER") == "true" {
  645. if cntrPidConvert != nil {
  646. hostPid, err := cntrPidConvert.getHostPidByHostProc(pid)
  647. if err != nil {
  648. log.Errorf("getHostPidByHostProc %d error:%v", pid, err)
  649. } else {
  650. log.Infof("getHostPidByHostProc %d hostPid:%d", pid, hostPid)
  651. }
  652. return hostPid, err
  653. }
  654. hostPid, err := getHostPidBySched(pid)
  655. if err != nil {
  656. log.Errorf("getHostPidBySched %d error:%v", pid, err)
  657. } else {
  658. log.Infof("getHostPidBySched %d hostPid:%d", pid, hostPid)
  659. }
  660. return hostPid, err
  661. } else {
  662. return pid, nil
  663. }
  664. }
  665. func getHostPidBySched(pid int) (int, error) {
  666. hostPid := 0
  667. schedFile := fmt.Sprintf("/proc/%d/sched", pid)
  668. file, err := os.Open(schedFile)
  669. if err != nil {
  670. return 0, err
  671. }
  672. defer file.Close()
  673. scanner := bufio.NewScanner(file)
  674. if scanner.Scan() {
  675. line := scanner.Text()
  676. re := regexp.MustCompile(`\d+`)
  677. match := re.FindString(line)
  678. if match != "" {
  679. hostPid, err = strconv.Atoi(match)
  680. if err != nil {
  681. return 0, err
  682. }
  683. }
  684. }
  685. return hostPid, nil
  686. }
  687. type cntrPidConvertMgr struct {
  688. cntrPidToHostPid map[int]int
  689. mtxCntrPidConvert *sync.RWMutex
  690. daemonid []byte
  691. }
  692. var cntrPidConvert *cntrPidConvertMgr
  693. func initCntrPidConvert() {
  694. if os.Getenv("CW_CONTAINER") == "true" && os.Getenv("CW_HOST_PID_ENABLE") != "true" {
  695. if hostPid, err := getHostPidBySched(1); err == nil && hostPid > 1 {
  696. log.Infof("not need convert cntrPid to hostPid")
  697. return
  698. }
  699. host_root := os.Getenv("HOST_ROOT")
  700. host_proc := os.Getenv("HOST_PROC")
  701. log.Infof("host_root %s, host_proc %s", host_root, host_proc)
  702. if len(host_proc) <= len(host_root) || host_proc[:len(host_root)] != host_root {
  703. log.Errorf("HOST_ROOT %s is not prefix of HOST_PROC %s", host_root, host_proc)
  704. return
  705. }
  706. //若host_proc目录不存在,直接返回
  707. if _, err := os.Stat(host_proc); os.IsNotExist(err) {
  708. log.Errorf("host_proc %s not exist", host_proc)
  709. return
  710. }
  711. daemonidPath := filepath.Join(GetDefaultPath(MetaPath, ".daemonid"))
  712. daemonid, err := ioutil.ReadFile(daemonidPath)
  713. if len(daemonid) == 0 {
  714. log.Errorf("read daemonid from %s fail: %v", daemonidPath, err)
  715. return
  716. }
  717. log.Infof("daemonid %s", daemonid)
  718. cntrPidConvert = &cntrPidConvertMgr{
  719. cntrPidToHostPid: make(map[int]int),
  720. mtxCntrPidConvert: &sync.RWMutex{},
  721. daemonid: daemonid,
  722. }
  723. }
  724. }
  725. func (c *cntrPidConvertMgr) getHostPidByHostProc(pid int) (int, error) {
  726. c.mtxCntrPidConvert.RLock()
  727. hostPid, ok := c.cntrPidToHostPid[pid]
  728. c.mtxCntrPidConvert.RUnlock()
  729. if ok {
  730. return hostPid, nil
  731. }
  732. var err error
  733. c.mtxCntrPidConvert.Lock()
  734. defer c.mtxCntrPidConvert.Unlock()
  735. hostPid, ok = c.cntrPidToHostPid[pid]
  736. if ok {
  737. return hostPid, nil
  738. }
  739. timeStart := time.Now()
  740. c.cntrPidToHostPid, err = parseProcDirectory(os.Getenv("OS_HOST_PROC"), c.daemonid)
  741. log.Infof("cntrPidToHostPid %#v, time_%s: %#v", c.cntrPidToHostPid, time.Since(timeStart).String(), err)
  742. if err != nil {
  743. return 0, err
  744. }
  745. hostPid, ok = c.cntrPidToHostPid[pid]
  746. if ok {
  747. return hostPid, nil
  748. } else {
  749. return 0, fmt.Errorf("can not find pid %d in container", pid)
  750. }
  751. }
  752. func parseProcDirectory(dirPath string, daemonid []byte) (pidMap map[int]int, err error) {
  753. pidMap = make(map[int]int)
  754. daemonidPath := filepath.Join(GetDefaultPath(MetaPath, ".daemonid"))
  755. fileInfo, err := os.Stat(daemonidPath)
  756. if err != nil {
  757. return
  758. }
  759. daemonidFileSize := fileInfo.Size()
  760. // 遍历指定目录下的PID目录
  761. files, err := ioutil.ReadDir(dirPath)
  762. if err != nil {
  763. return
  764. }
  765. for _, file := range files {
  766. if file.IsDir() {
  767. pid := file.Name()
  768. pidInt, err := strconv.Atoi(pid)
  769. if err != nil {
  770. continue
  771. }
  772. daemonidFilePath := filepath.Join(dirPath, pid, "root/opt/cloudwise/omniagent/meta/.daemonid")
  773. // 检查是否存在指定文件并size等于指定大小
  774. fileInfo, err = os.Stat(daemonidFilePath)
  775. if err != nil {
  776. continue
  777. }
  778. fileSize := fileInfo.Size()
  779. if fileSize != daemonidFileSize {
  780. log.Errorf("%s file size %d not equal %d", daemonidFilePath, fileSize, daemonidFileSize)
  781. continue
  782. }
  783. // 检查是否存在指定文件并内容等于指定内容
  784. daemonidContentBytes, err := ioutil.ReadFile(daemonidFilePath)
  785. if err == nil && bytes.Compare(daemonidContentBytes, daemonid) == 0 {
  786. statusFilePath := filepath.Join(dirPath, pid, "status")
  787. // 解析status文件获取NSpid的第二个字符串
  788. nspidValue, err := getNSpidValue(statusFilePath)
  789. if err == nil {
  790. pidMap[nspidValue] = pidInt
  791. } else {
  792. log.Errorf("getNSpidValue %s error:%v", statusFilePath, err)
  793. }
  794. }
  795. }
  796. }
  797. return pidMap, nil
  798. }
  799. func getNSpidValue(filePath string) (int, error) {
  800. content, err := ioutil.ReadFile(filePath)
  801. if err != nil {
  802. return 0, err
  803. }
  804. lines := strings.Split(string(content), "\n")
  805. for _, line := range lines {
  806. if strings.HasPrefix(line, "NSpid:") {
  807. fields := strings.Fields(line)
  808. if len(fields) >= 3 {
  809. valueStr := fields[2]
  810. value, err := strconv.Atoi(valueStr)
  811. if err != nil {
  812. return 0, fmt.Errorf("failed to convert NSpid value %s to integer: %v", valueStr, err)
  813. }
  814. return value, nil
  815. } else if len(fields) == 2 {
  816. valueStr := fields[1]
  817. value, err := strconv.Atoi(valueStr)
  818. if err != nil {
  819. return 0, fmt.Errorf("failed to convert NSpid value %s to integer: %v", valueStr, err)
  820. }
  821. return value, nil
  822. } else {
  823. log.Errorf("invalid NSpid line format in %s: %s", filePath, line)
  824. }
  825. }
  826. }
  827. return 0, errors.New("NSpid value not found!")
  828. }
  829. func ToString(v interface{}) string {
  830. b, err := json.Marshal(v)
  831. if err != nil {
  832. return err.Error()
  833. }
  834. return string(b)
  835. }
  836. func GetSoPath(pid uint32, soname string, rootfs string) (string, error) {
  837. // 容器进程的 so 文件无法通过裸路径访问,需加上根文件系统前缀。
  838. // 若调用方未提供 rootfs(metadata 不可用),fallback 到 /proc/<pid>/root,
  839. // 该路径在宿主机上始终可达,对宿主机进程同样无害(/proc/1/root == /)。
  840. effectiveRootfs := rootfs
  841. if effectiveRootfs == "" {
  842. effectiveRootfs = fmt.Sprintf("/proc/%d/root", pid)
  843. }
  844. mapsFile := fmt.Sprintf("/proc/%d/maps", pid)
  845. file, err := os.Open(mapsFile)
  846. if err != nil {
  847. return "", err
  848. }
  849. defer file.Close()
  850. scanner := bufio.NewScanner(file)
  851. for scanner.Scan() {
  852. line := scanner.Text()
  853. if strings.Contains(line, soname) {
  854. fields := strings.Fields(line)
  855. if len(fields) > 5 {
  856. path := fields[5]
  857. if strings.HasSuffix(path, ".so") {
  858. return filepath.Join(effectiveRootfs, path), nil
  859. }
  860. }
  861. }
  862. }
  863. return "", fmt.Errorf("library %s not found in process.", soname)
  864. }
  865. func StopTimerWithDrainChan(timer *time.Timer) {
  866. if !timer.Stop() {
  867. select {
  868. case <-timer.C:
  869. default:
  870. }
  871. }
  872. }
  873. func ResetTimerWithDrainChan(timer *time.Timer, d time.Duration) {
  874. if !timer.Stop() {
  875. select {
  876. case <-timer.C:
  877. default:
  878. }
  879. }
  880. timer.Reset(d)
  881. }
  882. func BytesToString(b []byte) string {
  883. return strings.TrimRight(string(b), "\x00")
  884. }