util.go 23 KB

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