這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
記錄一些go指令碼,使用定時器,訊號,go輕量級線程,調用系統命令,壓縮,計算md5,列表,擷取本機IP等功能
(向程式發送訊號如:kill -2 進程號)
package mainimport ("os"; "os/signal"; "os/exec""syscall""net""time""strconv"; "strings""bufio""io/ioutil""fmt""archive/zip""crypto/md5"l "container/list""bytes"//"encoding/hex""io")const (path string = "/root/logs/"log_pos string = "log_watcher.pos"log_access string = "access.log"log_upload string = "upload_access.log"salt string = "salt-value")var restarter int = 0var localip stringvar uploadfiles = l.New()//------------------------------------------------------------------------------func check(err error) bool {if err != nil {//panic(err)return false}return true}func get_local_ip() (string) {ifi, err := net.InterfaceByName("eth0")check(err)addrs, err := ifi.Addrs()check(err)for _,a := range addrs {if ip := strings.Split(a.String(),"/"); len(ip) > 1 {return ip[0]}}ifi, err = net.InterfaceByName("bond0")check(err)addrs, err = ifi.Addrs()check(err)for _,a := range addrs {if ip := strings.Split(a.String(),"/"); len(ip) > 1 {return ip[0]}}ifi, err = net.InterfaceByName("br0")check(err)addrs, err = ifi.Addrs()check(err)for _,a := range addrs {if ip := strings.Split(a.String(),"/"); len(ip) > 1 {return ip[0]}}panic("IP")}func load_data(filename string) (string) {buff, err := ioutil.ReadFile(filename)if !check(err) { return "" }return string(buff)}func save_data(filename, data string) {f, err := os.OpenFile(filename, os.O_WRONLY | os.O_CREATE | os.O_TRUNC , 0755)if !check(err) { return }defer f.Close()f.WriteString(data)}//------------------------------------------------------------------------------func restart() {ch := make(chan os.Signal, 1)signal.Notify(ch, syscall.SIGINT)for {<-chrestarter = 1}}//------------------------------------------------------------------------------func upload_append(filename, line string) {f, err := os.OpenFile(filename, os.O_RDWR | os.O_CREATE | os.O_APPEND , 0755)if !check(err) { return }defer f.Close()f.WriteString(line)}func worker(pos int64, line string) {upload_append(log_upload, line)if int(pos) % 10 ==0 {save_data(log_pos, strconv.Itoa(int(pos)))}}//------------------------------------------------------------------------------func compress() {// compresst := time.Now().Format("20060102_15_04")filename := path + "upload_access_" + t + ".zip"outf, err := os.Create(filename)if !check(err) { return }defer outf.Close()//defer os.Remove(filename)zw := zip.NewWriter(outf)f, err := zw.Create(log_upload)if !check(err) { os.Remove(filename); return }buff, err := ioutil.ReadFile(log_upload)if !check(err) { os.Remove(filename); return }_, err = f.Write(buff)if !check(err) { os.Remove(filename); return }zw.Close()os.Remove(log_upload)uploadfiles.PushFront(filename)}func upload() {file := uploadfiles.Back()if file != nil {filename,_ := file.Value.(string)ts := strconv.Itoa(int(time.Now().Unix()) - 8 * 3600)h := md5.New()io.WriteString(h, salt + string(ts))k := fmt.Sprintf("%x", h.Sum(nil))k = k[0:10]var out bytes.Buffercmd := exec.Command("/bin/sh","-c","curl -F \"action=upload\" -F \"t="+string(ts)+"\" -F \"k="+k+"\" -F \"f="+localip+"\" --limit-rate 50k --connect-timeout 60 --max-time 600 -F \"Filedata=@"+filename+"\" \"http://127.0.0.1/test.php\"")cmd.Stdout = &outerr := cmd.Run()if !check(err) { return }if out.String() == "1" {uploadfiles.Remove(file)os.Remove(filename)}}}func compress_timer() {for {timer := time.NewTicker(250 * time.Second)for {select {case <- timer.C:go compress()}}}}func upload_timer() {for {timer := time.NewTicker(30 * time.Second)for {select {case <- timer.C:upload()}}}}//------------------------------------------------------------------------------func main() {os.Chdir(path)//get local iplocalip = get_local_ip()//set timer to uploadgo compress_timer()go upload_timer()go restart()//load infocur_pos := load_data(log_pos)//read access logf, err := os.OpenFile(log_access, os.O_RDONLY, 0)if !check(err) { return }pos, _ := strconv.ParseInt(cur_pos, 0, 64)_, err = f.Seek(pos, os.SEEK_SET)if !check(err) { return }br := bufio.NewReader(f)for {line , err := br.ReadString('\n')pos, _ = f.Seek(0, os.SEEK_CUR)if err == io.EOF {time.Sleep(1 * time.Second)if restarter == 1 {f.Close()f, _ = os.OpenFile(log_access,os.O_RDONLY,0)_, err = f.Seek(0, os.SEEK_SET)br = bufio.NewReader(f)restarter = 0}} else {go worker(pos, line)}}}