modificata la struttura dei files
This commit is contained in:
parent
7c48a60d3b
commit
d80e25711d
3 changed files with 122 additions and 110 deletions
49
dbs.go
Normal file
49
dbs.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
// dbs
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/fzzy/radix/redis"
|
||||
"gopkg.in/mgo.v2"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Dbs struct {
|
||||
MongoUri string
|
||||
RedisUri string
|
||||
rdb *redis.Client
|
||||
mdb *mgo.Session
|
||||
ll *mgo.Collection
|
||||
}
|
||||
|
||||
type MongoLogin struct {
|
||||
User string `json:"user"`
|
||||
Protocol string `json:"protocol"`
|
||||
Ip string `json:"ip"`
|
||||
Date time.Time `json:"date"`
|
||||
}
|
||||
|
||||
type Index struct {
|
||||
User string `json:"user"`
|
||||
Date time.Time `json:"date"`
|
||||
}
|
||||
|
||||
func (db *Dbs) connectRedis() {
|
||||
var err error
|
||||
db.rdb, err = redis.Dial("tcp", db.RedisUri)
|
||||
if err != nil {
|
||||
log.Println("Redis connect Error: ", err.Error())
|
||||
os.Exit(-1)
|
||||
}
|
||||
}
|
||||
|
||||
func (db *Dbs) connectMongo() {
|
||||
var err error
|
||||
db.mdb, err = mgo.Dial(db.MongoUri)
|
||||
if err != nil {
|
||||
log.Println("Mongodb connect Error: ", err.Error())
|
||||
os.Exit(-3)
|
||||
}
|
||||
db.ll = db.mdb.DB("dovecot").C("lastlogin")
|
||||
}
|
|
@ -4,13 +4,8 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/fzzy/radix/redis"
|
||||
"gopkg.in/mgo.v2"
|
||||
"os"
|
||||
// "gopkg.in/mgo.v2/bson"
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
@ -23,32 +18,11 @@ type Options struct {
|
|||
CurrentPath string
|
||||
Exe string
|
||||
LogFile string
|
||||
PIDFile string
|
||||
Version bool
|
||||
}
|
||||
|
||||
type Dbs struct {
|
||||
MongoUri string
|
||||
RedisUri string
|
||||
rdb *redis.Client
|
||||
mdb *mgo.Session
|
||||
ll *mgo.Collection
|
||||
}
|
||||
|
||||
type MongoLogin struct {
|
||||
User string `json:"user"`
|
||||
Protocol string `json:"protocol"`
|
||||
Ip string `json:"ip"`
|
||||
Date time.Time `json:"date"`
|
||||
}
|
||||
|
||||
type Index struct {
|
||||
User string `json:"user"`
|
||||
Date time.Time `json:"date"`
|
||||
}
|
||||
|
||||
const (
|
||||
_VERSION = "v1.1.1"
|
||||
_VERSION = "v1.1.2"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -61,6 +35,8 @@ var (
|
|||
MongoUri: "mongodb://127.0.0.1:27018",
|
||||
RedisUri: "127.0.0.1:6379",
|
||||
}
|
||||
|
||||
pid = PID{}
|
||||
)
|
||||
|
||||
func usage() {
|
||||
|
@ -76,7 +52,7 @@ func init() {
|
|||
}
|
||||
|
||||
opts.LogFile = path.Join(opts.CurrentPath, opts.LogFile)
|
||||
opts.PIDFile = path.Join(opts.CurrentPath, "run", path.Base(os.Args[0])+".pid")
|
||||
pid.PIDFile = path.Join(opts.CurrentPath, "run", path.Base(os.Args[0])+".pid")
|
||||
opts.Exe = path.Base(os.Args[0])
|
||||
|
||||
flag.StringVar(&dbs.MongoUri, "m", dbs.MongoUri, "Mongodb")
|
||||
|
@ -86,54 +62,6 @@ func init() {
|
|||
flag.BoolVar(&opts.Version, "v", false, "Version")
|
||||
}
|
||||
|
||||
func (db *Dbs) connectRedis() {
|
||||
var err error
|
||||
db.rdb, err = redis.Dial("tcp", db.RedisUri)
|
||||
if err != nil {
|
||||
log.Println("Redis connect Error: ", err.Error())
|
||||
os.Exit(-1)
|
||||
}
|
||||
}
|
||||
|
||||
func (db *Dbs) connectMongo() {
|
||||
var err error
|
||||
db.mdb, err = mgo.Dial(db.MongoUri)
|
||||
if err != nil {
|
||||
log.Println("Mongodb connect Error: ", err.Error())
|
||||
os.Exit(-3)
|
||||
}
|
||||
db.ll = db.mdb.DB("dovecot").C("lastlogin")
|
||||
}
|
||||
|
||||
func checkPID(bpid []byte) bool {
|
||||
pid := strings.TrimRight(string(bpid), "\n")
|
||||
fmt.Println("PID: ", pid)
|
||||
bcmd, err := ioutil.ReadFile(path.Join("/proc", pid, "cmdline"))
|
||||
if err != nil {
|
||||
fmt.Println("cmdline error: ", err)
|
||||
return false
|
||||
}
|
||||
cmd := bytes.Trim(bcmd, "\x00")
|
||||
// fmt.Println(string(cmd), opts.Exe)
|
||||
if strings.Contains(string(cmd), opts.Exe) {
|
||||
return true
|
||||
} else {
|
||||
fmt.Printf("PID %s used by %s\n", pid, cmd)
|
||||
return true
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func writePID(pfile string) {
|
||||
fpid, err := os.OpenFile(pfile, os.O_WRONLY|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
fmt.Println("PID file error: ", err.Error())
|
||||
os.Exit(-5)
|
||||
}
|
||||
fpid.WriteString(strconv.Itoa(os.Getpid()))
|
||||
fpid.Close()
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Usage = usage
|
||||
flag.Parse()
|
||||
|
@ -143,15 +71,8 @@ func main() {
|
|||
os.Exit(0)
|
||||
}
|
||||
|
||||
if bpid, err := ioutil.ReadFile(opts.PIDFile); err == nil && checkPID(bpid) {
|
||||
fmt.Println("Running: ", string(bpid))
|
||||
os.Exit(-6)
|
||||
} else {
|
||||
writePID(opts.PIDFile)
|
||||
}
|
||||
defer os.Remove(opts.PIDFile)
|
||||
|
||||
// fmt.Println(os.Stat(opts.PIDFile))
|
||||
pid.Write()
|
||||
defer pid.Remove()
|
||||
|
||||
fs, err := os.OpenFile(opts.LogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
|
@ -161,7 +82,6 @@ func main() {
|
|||
defer fs.Close()
|
||||
|
||||
log.SetOutput(fs)
|
||||
// log.SetPrefix("[llmongo] ")
|
||||
|
||||
start := time.Now()
|
||||
fmt.Printf("Start: %+v\n", opts)
|
||||
|
@ -173,16 +93,6 @@ func main() {
|
|||
dbs.connectMongo()
|
||||
defer dbs.mdb.Close()
|
||||
|
||||
// // Estrae la lista degli utenti che hanno fatto login negli ultimi X min.
|
||||
// llindex := opts.rdb.Cmd("smembers", "llindex")
|
||||
// lista, err := llindex.List()
|
||||
// if err != nil {
|
||||
// log.Panicln("LLINDEX error: ", err.Error())
|
||||
// }
|
||||
// // for _, user := range lista {
|
||||
// // cicla fino a che esistono righe di login
|
||||
|
||||
// var wg sync.WaitGroup
|
||||
count := 0
|
||||
for {
|
||||
// estrae un userid dalla lista degli utenti che hanno fatto login
|
||||
|
@ -194,8 +104,6 @@ func main() {
|
|||
log.Printf("LLINDEX empty: %v\n", err)
|
||||
break
|
||||
}
|
||||
count++
|
||||
// user := spop.String()
|
||||
var date int64
|
||||
var lastval, val string
|
||||
for {
|
||||
|
@ -244,7 +152,7 @@ func main() {
|
|||
Date: time.Unix(date, 0),
|
||||
}
|
||||
// inserisce il login su Mongodb
|
||||
|
||||
count++
|
||||
_, err := dbs.ll.Upsert(ind, ml)
|
||||
if err != nil {
|
||||
log.Printf("Insert error: %+v\n", err)
|
||||
|
@ -253,21 +161,11 @@ func main() {
|
|||
continue
|
||||
}
|
||||
}
|
||||
// fmt.Println("Info: ", info)
|
||||
// cancella da Redis la riga di login inserita
|
||||
retval := dbs.rdb.Cmd("lrem", user, "-1", val)
|
||||
log.Println("LREM retval: ", retval, user, val)
|
||||
lastval = val
|
||||
}
|
||||
// controlla se ci sono ancora line di login per l'utente 'user'
|
||||
/*
|
||||
llen, _ := opts.rdb.Cmd("llen", user).Int64()
|
||||
if llen <= 1 {
|
||||
// elimina l'utente dalla lista di quelli che hanno fatto login se ci sono 1 o meno
|
||||
retval := opts.rdb.Cmd("srem", "llindex", user)
|
||||
log.Println("SREM retval: ", retval, user)
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
fmt.Printf("Stop %v - %d\n", time.Since(start), count)
|
65
pid.go
Normal file
65
pid.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
// pid
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type PID struct {
|
||||
PID string
|
||||
PIDFile string
|
||||
}
|
||||
|
||||
func (p *PID) check() bool {
|
||||
bpid, err := ioutil.ReadFile(p.PIDFile)
|
||||
p.PID = strings.TrimRight(string(bpid), "\n")
|
||||
if err == nil && p.readCmd() {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *PID) readCmd() bool {
|
||||
bcmd, err := ioutil.ReadFile(path.Join("/proc", p.PID, "cmdline"))
|
||||
if err != nil {
|
||||
fmt.Println("cmdline error: ", err)
|
||||
return false
|
||||
}
|
||||
cmd := bytes.Trim(bcmd, "\x00")
|
||||
if strings.Contains(string(cmd), opts.Exe) {
|
||||
return true
|
||||
} else {
|
||||
fmt.Printf("PID %s used by %s\n", pid, cmd)
|
||||
return true
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (p *PID) Write() {
|
||||
|
||||
if p.check() {
|
||||
fmt.Println("Running: ", p.PID)
|
||||
os.Exit(-6)
|
||||
}
|
||||
|
||||
fpid, err := os.OpenFile(p.PIDFile, os.O_WRONLY|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
fmt.Println("PID file error: ", err.Error())
|
||||
os.Exit(-5)
|
||||
}
|
||||
fpid.WriteString(strconv.Itoa(os.Getpid()))
|
||||
fpid.Close()
|
||||
}
|
||||
|
||||
func (p *PID) Remove() {
|
||||
err := os.Remove(p.PIDFile)
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue