Skip to content

Commit 7b3dc4a

Browse files
committed
#Code commit.
1 parent ea8fc05 commit 7b3dc4a

File tree

13 files changed

+372
-0
lines changed

13 files changed

+372
-0
lines changed

.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.[oa]
2+
*~
3+
*.exe
4+
.idea
5+
bin/
6+
pkg/
7+
**/_.githooks/
8+
**/_.git/
9+
tests
10+
*.a

src/agent/conf/app.conf

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#cronjob spec
2+
schedule=*/10 * * * *
3+
4+
#command type, only FILE and BASH supported.
5+
type=FILE
6+
7+
#command to be executed. You can use a remote file, or just a command, such as: ls -alh .
8+
command=http://10.45.11.194:45520/test.sh
9+
10+
#when command type is FILE, file body encrypted or not .
11+
encrypted=true
12+
13+
#Encrypted salt key, limit to 16 bytes.
14+
encryptedKey=TestTestTestTest

src/agent/conf/constant.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package conf
2+
3+
var CronSchedule = "schedule"
4+
var CommandType = "type"
5+
var Command = "command"
6+
var Encrypted = "encrypted"
7+
var EncryptedKey = "encryptedKey"

src/agent/main.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"agent/service"
5+
"agent/utill"
6+
"github.com/robfig/cron"
7+
"log"
8+
"os"
9+
)
10+
11+
func main() {
12+
13+
schedule, commandType, command := utill.ParseLaunchArgs()
14+
15+
cronJob := cron.New()
16+
cronJob.AddFunc(schedule, func() {
17+
18+
defer func() {
19+
if err := recover(); err != nil {
20+
log.Print(err)
21+
os.Exit(-1)
22+
}
23+
}()
24+
25+
service.ExecCommand(commandType, command)
26+
27+
})
28+
29+
cronJob.Start()
30+
31+
select {}
32+
}

src/agent/service/CronService.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package service
2+
3+
import "log"
4+
5+
func init() {
6+
7+
}
8+
9+
func LaunchCronJob(cronStr string) {
10+
11+
log.Print("start cronjob")
12+
13+
log.Print("Cronjob in progress")
14+
}

src/agent/service/ExecService.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package service
2+
3+
import (
4+
"agent/utill"
5+
"log"
6+
"os/exec"
7+
"strings"
8+
)
9+
10+
func ExecCommand(commandType string, command string) {
11+
var cmd *exec.Cmd
12+
13+
commandType = strings.ToUpper(commandType)
14+
15+
switch commandType {
16+
case "BASH":
17+
cmd = exec.Command("sh", "-c", command)
18+
case "FILE":
19+
20+
if strings.HasSuffix(command, ".py") {
21+
cmd = exec.Command("python", command)
22+
} else if strings.HasSuffix(command, ".sh") {
23+
24+
if strings.HasPrefix(command, "http") {
25+
26+
fileBody := utill.GetRemoteFileBody(command)
27+
28+
cmd = exec.Command("sh", "-c", fileBody)
29+
30+
} else {
31+
cmd = exec.Command("sh", "-c", command)
32+
33+
}
34+
35+
} else {
36+
panic("Unknown file type! " + command)
37+
}
38+
39+
default:
40+
cmd = exec.Command("sh", "-c", "echo 'loop once, no command found!'")
41+
}
42+
43+
out, err := cmd.Output()
44+
45+
if err != nil {
46+
log.Print(err.Error())
47+
}
48+
49+
log.Printf("%s", string(out))
50+
}

src/agent/startAgent.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
echo
3+
4+
ls -alh
5+
date

src/agent/test/Aes_Test.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package test
2+
3+
func init() {
4+
5+
}

src/agent/test/fileTest.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package test
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"log"
7+
"net/http"
8+
"os/exec"
9+
)
10+
11+
func main() {
12+
13+
//executeRemoteFile()
14+
15+
//ParseAppConf()
16+
17+
}
18+
func executeRemoteFile() {
19+
var url = "http://10.45.11.194:45520/test.sh"
20+
resp, err := http.Get(url)
21+
if err != nil {
22+
log.Print(err.Error())
23+
}
24+
defer resp.Body.Close()
25+
data, _ := ioutil.ReadAll(resp.Body)
26+
fmt.Println(string(data))
27+
//os
28+
//cmd
29+
//exec
30+
cmd := exec.Command("sh", "-c", string(data))
31+
out, err := cmd.Output()
32+
if err != nil {
33+
fmt.Print(err.Error())
34+
}
35+
fmt.Print(string(out))
36+
}

src/agent/utill/aesUtil.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package utill
2+
3+
import (
4+
"agent/conf"
5+
"crypto/aes"
6+
"crypto/cipher"
7+
"crypto/rand"
8+
"encoding/hex"
9+
"errors"
10+
"io"
11+
"log"
12+
)
13+
14+
// salt key length should limit to 8 char.
15+
var saltKey []byte
16+
17+
func init() {
18+
saltKey = []byte("saltDataSaltData")
19+
cMap := ParseAppConf()
20+
21+
if cMap != nil && cMap[conf.Encrypted] == "true" {
22+
saltKey = []byte(cMap[conf.EncryptedKey])
23+
}
24+
}
25+
26+
// Encrypt data using AES algorithm
27+
func EncryptAesData(textData string) (string, error) {
28+
block, err := aes.NewCipher(saltKey)
29+
30+
if err != nil {
31+
log.Fatal(err.Error())
32+
return "", err
33+
}
34+
35+
cipherText := make([]byte, aes.BlockSize+len(textData))
36+
initVector := cipherText[:aes.BlockSize]
37+
if _, err := io.ReadFull(rand.Reader, initVector); err != nil {
38+
return "", err
39+
}
40+
cipher.NewCFBEncrypter(block, initVector).XORKeyStream(cipherText[aes.BlockSize:], []byte(textData))
41+
return hex.EncodeToString(cipherText), nil
42+
43+
}
44+
45+
// Decrypt data using AES algorithm.
46+
func DecryptAesData(encryptData string) (string, error) {
47+
cipherText, _ := hex.DecodeString(encryptData)
48+
49+
block, err := aes.NewCipher(saltKey)
50+
if err != nil {
51+
return "", err
52+
}
53+
if len(cipherText) < aes.BlockSize {
54+
return "", errors.New("cipherText too short")
55+
}
56+
initVector := cipherText[:aes.BlockSize]
57+
cipherText = cipherText[aes.BlockSize:]
58+
cipher.NewCFBDecrypter(block, initVector).XORKeyStream(cipherText, cipherText)
59+
return string(cipherText), nil
60+
}

src/agent/utill/argUtil.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package utill
2+
3+
import (
4+
"agent/conf"
5+
"fmt"
6+
"log"
7+
"os"
8+
)
9+
10+
var defaultSchedule = "*/5 * * * *"
11+
var defaultType = "FILE"
12+
var defaultCommand = "startAgent.sh"
13+
14+
func Help() {
15+
fmt.Println("************ Ops Monitor Agent ************\n")
16+
fmt.Printf("\t%s\t%s\n", "-s", " cronjob schedule. Such as -s=*/5 * * * * , execute for each 5 seconds.")
17+
fmt.Printf("\t%s\t%s\n", "-t", " cronjob command type. Such as FILE, BASH.")
18+
fmt.Printf("\t%s\t%s\n", "-c", " command to be execute, such as date, ls, mv, test.sh.")
19+
fmt.Printf("\t%s\t%s\n", "-h", " ops agent, help tips.")
20+
}
21+
22+
func ParseLaunchArgs() (schedule string, commandType string, command string) {
23+
24+
args := os.Args
25+
26+
if len(args) > 1 && (args[1] == "-h" || args[1] == "--help") {
27+
Help()
28+
return
29+
}
30+
31+
log.Print("****** Launch ops-monitor agent ******")
32+
33+
configMap := ParseAppConf()
34+
35+
if configMap[conf.CronSchedule] != "" {
36+
defaultSchedule = configMap[conf.CronSchedule]
37+
}
38+
39+
if configMap[conf.CommandType] != "" {
40+
defaultType = configMap[conf.CommandType]
41+
}
42+
43+
if configMap[conf.Command] != "" {
44+
defaultCommand = configMap[conf.Command]
45+
}
46+
47+
return defaultSchedule, defaultType, defaultCommand
48+
}

src/agent/utill/fileUtil.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package utill
2+
3+
import (
4+
"agent/conf"
5+
"io/ioutil"
6+
"log"
7+
"net/http"
8+
"os"
9+
"path/filepath"
10+
"strings"
11+
"sync"
12+
)
13+
14+
var cMap = make(map[string]string)
15+
var mutex = sync.Mutex{}
16+
17+
func GetRemoteFileBody(filePath string) string {
18+
19+
log.Print("begin download file:", filePath)
20+
resp, err := http.Get(filePath)
21+
22+
if err != nil {
23+
panic(err.Error())
24+
}
25+
26+
defer resp.Body.Close()
27+
data, _ := ioutil.ReadAll(resp.Body)
28+
29+
fileBody := string(data)
30+
31+
conMap := ParseAppConf()
32+
if conMap[conf.Encrypted] == "true" {
33+
34+
fileBody = strings.Replace(fileBody, "\n", "", -1)
35+
fileBody = strings.Replace(fileBody, "\r", "", -1)
36+
fileBody, _ = DecryptAesData(fileBody)
37+
}
38+
39+
return fileBody
40+
}
41+
42+
func ParseAppConf() map[string]string {
43+
44+
var configPath string
45+
46+
mutex.Lock()
47+
defer mutex.Unlock()
48+
49+
if len(cMap) != 0 {
50+
return cMap
51+
}
52+
53+
home, _ := os.Getwd()
54+
configPath = filepath.Join(home, "src/agent/conf", "app.conf")
55+
56+
if !FileExists(configPath) {
57+
configPath = filepath.Join(home, "conf", "app.conf")
58+
59+
if !FileExists(configPath) {
60+
panic("app.conf not found!" + configPath)
61+
}
62+
}
63+
64+
cFile, _ := os.Open(configPath)
65+
fileBytes, _ := ioutil.ReadAll(cFile)
66+
fileBody := string(fileBytes)
67+
68+
fileBody = strings.Replace(fileBody, "\r", "", -1)
69+
bodyArr := strings.Split(fileBody, "\n")
70+
71+
for _, body := range bodyArr {
72+
if body != "" && !strings.HasPrefix(body, "#") {
73+
arrV := strings.Split(body, "=")
74+
if len(arrV) == 2 {
75+
cMap[arrV[0]] = arrV[1]
76+
}
77+
}
78+
}
79+
80+
return cMap
81+
}
82+
83+
func FileExists(name string) bool {
84+
if _, err := os.Stat(name); err != nil {
85+
if os.IsNotExist(err) {
86+
return false
87+
}
88+
}
89+
return true
90+
}

src/github.com/robfig/cron

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit b41be1df696709bb6395fe435af20370037c0b4c

0 commit comments

Comments
 (0)