Go language four-script two

Source: Internet
Author: User
Tags readfile
This is a creation in Article, where the information may have evolved or changed.

Record some go scripts, use timers, signals, go lightweight threads, call system commands, compress, calculate MD5, list, get native IP and other functions

(Send a signal to the program such as: KILL-2 process number)

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_a Ccess.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") {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_ap Pend (Log_upload, line) if int (POS)% ==0 {Save_data (Log_poS, StrConv. Itoa (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--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 (* time. Second) for {select {case <-timer. C:go compress ()}}}}func Upload_timer () {for {timer: = time. Newticker (* 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, +) _, 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)}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.