Skip to content

Commit c1dd894

Browse files
김경남김경남
김경남
authored and
김경남
committed
initial commit
1 parent ed949ec commit c1dd894

File tree

12 files changed

+327
-0
lines changed

12 files changed

+327
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
debug
2+
.swp
3+
test/*
4+
**/*.scouter

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# argos
2+
3+
arogs is mariadb agent for scouter.

_argos.home/.DS_Store

6 KB
Binary file not shown.

_argos.home/conf/scouter.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"configurations": {
3+
"collector.ip": "127.0.0.1",
4+
"collector.udp.port": "6100",
5+
"collector.tcp.port": "6100",
6+
"db.instances": [
7+
{
8+
"db.ip": "db addr",
9+
"db.port": "3306",
10+
"db.user": "user_name",
11+
"db.password": "user_password",
12+
"db.slowquery": "/path/to/slow_query"
13+
},
14+
{
15+
"db.server": "db addr",
16+
"db.port": "3308",
17+
"db.user": "user_name",
18+
"db.password": "user_password",
19+
"db.slowquery": "/path/to/slow_query"
20+
}
21+
]
22+
}
23+
}

agent/config/configuration.go

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package config
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"os"
7+
"sync"
8+
"time"
9+
)
10+
11+
type Configuration struct {
12+
stopRunning chan bool
13+
}
14+
15+
var confFilePath string
16+
var confFile *os.File
17+
var running = make(chan bool)
18+
var confFileModdTime time.Time
19+
var confFileSize int64
20+
21+
type ConfObject struct {
22+
Configurations Collector
23+
}
24+
25+
type Collector struct {
26+
IP string `json:"collector.ip"`
27+
Udpport string `json:"collector.udp.port"`
28+
Tcpport string `json:"collector.tcp.port"`
29+
Instances []DBInstance `json:"db.instances"`
30+
}
31+
32+
type DBInstance struct {
33+
IP string `json:"db.ip"`
34+
Port string `json:"db.port"`
35+
User string `json:"db.user"`
36+
Password string `json:"db.password"`
37+
Slowquery string `json:"db.slowquery"`
38+
}
39+
40+
func (conf *Configuration) load() {
41+
var confobj ConfObject
42+
fileInfo, e := confFile.Stat()
43+
if e != nil {
44+
//todo: error handling
45+
}
46+
47+
if confFileModdTime != fileInfo.ModTime() || confFileSize != fileInfo.Size() {
48+
file, err := ioutil.ReadFile(confFilePath)
49+
if err != nil {
50+
//todo :
51+
}
52+
json.Unmarshal(file, &confobj)
53+
confFileSize = fileInfo.Size()
54+
confFileModdTime = fileInfo.ModTime()
55+
}
56+
57+
}
58+
59+
func (conf *Configuration) init() bool {
60+
f, err := os.Open(confFilePath)
61+
if err != nil {
62+
return false
63+
} else {
64+
confFile = f
65+
}
66+
return true
67+
}
68+
69+
func (conf *Configuration) Start() {
70+
go conf.run()
71+
}
72+
73+
func (conf *Configuration) Stop() {
74+
conf.stopRunning <- true
75+
}
76+
77+
func (conf *Configuration) run() {
78+
for {
79+
conf.load()
80+
time.Sleep(1 * time.Second)
81+
select {
82+
case <-conf.stopRunning:
83+
break
84+
default:
85+
continue
86+
}
87+
}
88+
}
89+
90+
var configure *Configuration
91+
var once sync.Once
92+
93+
// GetInstance returns configuraton singleton instance
94+
func GetInstance() *Configuration {
95+
once.Do(func() {
96+
configure = &Configuration{}
97+
if configure.init() {
98+
configure.Start()
99+
}
100+
})
101+
return configure
102+
}

agent/main/app.go

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strconv"
7+
"strings"
8+
"time"
9+
10+
"github.com/scouter-argos/agent/config"
11+
"github.com/scouter-argos/agent/manager"
12+
)
13+
14+
var pidfile string
15+
var f *os.File
16+
17+
func main() {
18+
if len(os.Args) > 1 {
19+
readArgs(os.Args)
20+
}
21+
start()
22+
23+
}
24+
25+
func start() {
26+
displayLogo()
27+
//workGroup.Add(1)
28+
//var conf *config.Configuration
29+
config.GetInstance()
30+
manager.ServiceStart()
31+
writePid()
32+
33+
for fileExist(pidfile) {
34+
time.Sleep(1 * time.Second)
35+
}
36+
37+
return
38+
}
39+
40+
func stop() {
41+
if fileExist(pidfile) {
42+
os.Remove(pidfile)
43+
} else {
44+
fmt.Println("Agent is not running.")
45+
}
46+
}
47+
48+
func displayLogo() {
49+
fmt.Println("MySQL Agent.")
50+
}
51+
52+
func fileExist(filename string) bool {
53+
if _, err := os.Stat(filename); err != nil {
54+
if os.IsNotExist(err) {
55+
return false
56+
}
57+
}
58+
return true
59+
}
60+
61+
func writePid() error {
62+
path, err := os.Getwd()
63+
if err == nil {
64+
pidfile = path + "/" + strconv.Itoa(os.Getpid()) + ".scouter"
65+
f, err = os.Create(pidfile)
66+
if err != nil {
67+
return err
68+
}
69+
defer f.Close()
70+
} else {
71+
return nil
72+
}
73+
return nil
74+
}
75+
76+
func usage() {
77+
fmt.Printf("need arguements")
78+
}
79+
80+
func readArgs(args []string) {
81+
for _, value := range args {
82+
argsItem := strings.Split(value, "=")
83+
if argsItem[0] == "scouter.config" {
84+
config.ConfFilePath = argsItem[1]
85+
}
86+
}
87+
}

agent/manager/service_manager.go

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

common/dataio/datainputx.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package dataio

common/dataio/dataoutputx.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package dataio
2+
3+
import (
4+
"bytes"
5+
"encoding/binary"
6+
"fmt"
7+
)
8+
9+
/*var INT3_MIN_VALUE int = 0xff800000
10+
var INT3_MAX_VALUE int = 0x007fffff
11+
var LONG5_MIN_VALUE int64 = 0xffffff8000000000
12+
var LONG5_MAX_VALUE int64 = 0x0000007fffffffff */
13+
14+
// A DataOutputX is a output stream which used write various kinds of data.
15+
type DataOutputX struct {
16+
written int // the wrtten bytes.
17+
buffer *bytes.Buffer
18+
}
19+
20+
// NewDataOutputX returns DataOutputX object
21+
func NewDataOutputX() *DataOutputX {
22+
out := new(DataOutputX)
23+
out.written = 0
24+
out.buffer = new(bytes.Buffer)
25+
return out
26+
}
27+
28+
// WriteInt32 write int32 number to buffer.
29+
func (out *DataOutputX) WriteInt32(value int32) *DataOutputX {
30+
out.written += 4
31+
err := binary.Write(out.buffer, binary.BigEndian, value)
32+
if err != nil {
33+
fmt.Println("Failed to binary write : ", err)
34+
}
35+
return out
36+
}
37+
38+
// WriteInt16 write int16 number to buffer.
39+
func (out *DataOutputX) WriteInt16(value int16) *DataOutputX {
40+
out.written += 2
41+
err := binary.Write(out.buffer, binary.BigEndian, value)
42+
if err != nil {
43+
fmt.Println("Failed to binary write : ", err)
44+
}
45+
return out
46+
}
47+
48+
// WriteInt64 write int64 number to buffer.
49+
func (out *DataOutputX) WriteInt64(value int64) *DataOutputX {
50+
out.written += 8
51+
err := binary.Write(out.buffer, binary.BigEndian, value)
52+
if err != nil {
53+
fmt.Println("Failed to binary write : ", err)
54+
}
55+
return out
56+
}
57+
58+
//ReadInt32 reads int32 number from buffer and assign to value.
59+
func (out *DataOutputX) ReadInt32(value *int32) {
60+
err := binary.Read(out.buffer, binary.BigEndian, value)
61+
if err != nil {
62+
fmt.Println("Faileed to binary read :", err)
63+
value = nil
64+
}
65+
66+
}

common/dataio/dataoutputx_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package dataio
2+
3+
import "testing"
4+
5+
func TestWriteInt(t *testing.T) {
6+
out := NewDataOutputX()
7+
out.WriteInt32(100)
8+
out.WriteInt32(200)
9+
var a int32
10+
out.ReadInt32(&a)
11+
t.Log("the value of a : %d", a)
12+
out.ReadInt32(&a)
13+
t.Log("the value of a : %d", a)
14+
}

common/main/dataio-main.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/iwindfree/argos/common/dataio"
7+
)
8+
9+
func main() {
10+
out := dataio.NewDataOutputX()
11+
out.WriteInt32(10)
12+
out.WriteInt32(100)
13+
out.WriteInt32(200)
14+
var value int32
15+
out.ReadInt32(&value)
16+
fmt.Println("the value of a : %d", value)
17+
out.ReadInt32(&value)
18+
fmt.Println("the value of a : %d", value)
19+
out.ReadInt32(&value)
20+
fmt.Println("the value of a : %d", value)
21+
22+
}

common/main/main.exe

1.91 MB
Binary file not shown.

0 commit comments

Comments
 (0)