Skip to content

Instantly share code, notes, and snippets.

@hirokazumiyaji
Created June 8, 2017 05:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirokazumiyaji/87342dd7270b54a1d0cc3fd864db55e2 to your computer and use it in GitHub Desktop.
Save hirokazumiyaji/87342dd7270b54a1d0cc3fd864db55e2 to your computer and use it in GitHub Desktop.
package main
type Client struct {
Service *ec2.Ec2
InstanceId string
}
func newClient() (*Client, error) {
s := session.New()
md := ec2metadata.New(s)
region, err := md.Region()
if err != nil {
return nil, err
}
id, err := md.GetMetadata("instance-id")
if err != nil {
return nil, err
}
return &Client{
Service: ec2.New(
s,
&aws.Config{Credentials: ec2rolecread.NewCredentialsWithClient(md), aws.String(region)},
),
InstanceId: id,
}
}
func (c *Client) createTag(t *ec2.Tag) error {
_, err := c.svc.CreateTags(c.params(t))
return err
}
func (c *Client) params(t *ec2.Tag) *ec2.CreateTagsInput {
return &ec2.CreateTagsInput{
Resources: []*string{
aws.String(c.id),
},
Tags: []*ec2.Tag{t},
DryRun: aws.Bool(false),
}
}
package main
import (
"errors"
"time"
"github.com/BurntSushi/toml"
"github.com/aws/aws-sdk-go/service/ec2"
)
type Config struct {
TailFile string `toml:"tail_file"`
PostionFile string `toml:"postion_file"`
SearchStart string `toml:"search_Start"`
SearchEnd string `toml:"search_end"`
TagName string `toml:"tag_name"`
TagStartValue string `toml:"tag_start_value"`
TagEndValue string `toml:"tag_end_value"`
Delay time.Duration `toml:"delay"`
}
func loadConfig(p string) (*Config, error) {
c := new(Config)
_, err := toml.DecodeFile(p, c)
if err != nil {
return nil, err
}
// check error.
if c.TailFile == "" {
return nil, errors.New("please set tailfile.")
}
if c.PostionFile == "" {
return nil, errors.New("please set postion file.")
}
if c.SearchStart == "" {
return nil, errors.New("please set search start.")
}
if c.SearchEnd == "" {
return nil, errors.New("please set search end.")
}
return c, nil
}
func (c *Config) StartTag() *ec2.Tag {
return &ec2.Tag{
Key: c.TagName,
Value: c.TagStartValue,
}
}
func (c *Config) EndTag() *ec2.Tag {
return &ec2.Tag{
Key: c.TagName,
Value: c.TagEndValue,
}
}
package main
import (
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/hpcloud/tail"
)
const defaultConfigPath = "/usr/local/src/monitor.toml"
// Get file size.
func getFileSize(p string) (int64, error) {
f, err := os.Open(p)
defer f.Close()
if err != nil {
return 0, err
}
s, err := f.Stat()
if err != nil {
return 0, err
}
return s.Size(), nil
}
func main() {
var configPath string
flag.StringVar(&configPath, "config", defaultConfigPath, "config path")
flag.StringVar(&configPath, "c", defaultConfigPath, "config path")
flag.Parse()
config, err := loadConfig(configPath)
if err != nil {
fmt.Println(err.Error())
return
}
client, err := newClient()
if err != nil {
fmt.Println(err)
return
}
pf := newPosFile(config.PositionFile)
postion, err := getFileSize(config.TailFile)
if err != nil {
fmt.Println(err)
return
}
offset := position
if pf.exist() {
offset, err = pf.read()
if err != nil {
fmt.Println(err)
return
}
}
tc := tail.Config{
Follow: true,
Location: &tail.SeekInfo{
Offset: offset,
Whence: os.SEEK_SET,
},
}
t, err := tail.TailFile(config.TailFile, tc)
if err != nil {
fmt.Println(err)
return
}
for line := range t.Lines {
postion += int64(len(line.Text) + 1)
if err := pf.touch(pos); err != nil {
fmt.Println(err)
return
}
err = nil
if strings.Index(line.Text, config.SearchStart) != -1 {
time.Sleep(config.Delay)
err = client.createTag(config.StartTag())
} else if strings.Index(line.Text, confgi.SearchEnd) != -1 {
time.Sleep(config.Delay)
err = client.createTag(config.EndTag())
}
if err != nil {
fmt.Println(err)
}
}
}
package main
import (
"io/ioutil"
"os"
"strconv"
)
type PosFile struct {
filePath string
}
func newPosFile(p string) *PosFile {
return &PosFile{
filePath: p,
}
}
func (p *PosFile) exist() bool {
_, err := os.Stat(p.filePath)
return err == nil
}
func (p *PosFile) read() (int64, error) {
f, err := os.Open(p.filePath)
if err != nil {
return int64(0), err
}
defer f.Close()
bs, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
return strconv.ParseInt(string(bs), 0, 64)
}
func (p *PosFile) touch(pos int64) error {
return ioutil.WriteFile(p.filePath, []byte(strconv.FormatInt(pos, 10)), os.ModePerm)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment