Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
neko-neko committed Apr 3, 2017
0 parents commit 4b375b5
Show file tree
Hide file tree
Showing 11 changed files with 513 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
### https://raw.github.com/github/gitignore/20cd385794419463136c26d8e76c754fbf2d0678/Go.gitignore

# Binaries for programs and plugins
*.exe
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/


### https://raw.github.com/github/gitignore/20cd385794419463136c26d8e76c754fbf2d0678/Global/MacOS.gitignore

*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk


Expand Down
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 neko-neko

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# utmpdump
utmpdump is output utmp format file to json or tsv or csv.

## Installation
**[Download latest binary](https://github.com/neko-neko/utmpdump/releases/latest)**

Multi-platform support
- Windows
- Mac OS X
- Linux 32,64bit

Or get the library
```
$ go get github.com/neko-neko/utmpdump
```
Or clone the repository and run
```
$ go install github.com/neko-neko/utmpdump
```

## Usage
```
Usage of utmpdump: utmpdump [options]
-f, --file <file> load a specific file instead of utmp
-t, --until <YYYYMMDDHHMMSS> display the lines until the specified time
-s, --since <YYYYMMDDHHMMSS> display the lines since the specified time
-o, --output <json/tsv/csv> display the lines at specific format. Default format is json.
```

### Example
```
$ utmpdump -f /var/log/wtmp
```

## Contributing
1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request :D

## Credits
neko-neko

## License
MIT
42 changes: 42 additions & 0 deletions filter/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package filter

import (
"time"

"github.com/neko-neko/utmpdump/utmp"
)

type TimeFilter struct{}

// filter time
func (f *TimeFilter) Filter(utmps []*utmp.GoUtmp, since time.Time, until time.Time) []*utmp.GoUtmp {
var (
result = []*utmp.GoUtmp{}
sinceIsZero = since.IsZero()
untilIsZero = until.IsZero()
)

if sinceIsZero && untilIsZero {
return utmps
}

if !sinceIsZero {
for _, u := range utmps {
utime, _ := time.Parse(utmp.TimeFormat, u.Time)
if !utime.Before(since) {
result = append(result, u)
}
}
}

if !untilIsZero {
for _, u := range utmps {
utime, _ := time.Parse(utmp.TimeFormat, u.Time)
if !utime.After(until) {
result = append(result, u)
}
}
}

return result
}
82 changes: 82 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"flag"
"fmt"
"os"
"strings"
"time"

"github.com/neko-neko/utmpdump/filter"
"github.com/neko-neko/utmpdump/printer"
"github.com/neko-neko/utmpdump/utmp"
)

func main() {
var (
filePath string
until string
since string
outputType string
)

flag.Usage = func() {
fmt.Fprintf(os.Stderr, `Usage of %s: %s [options]
-f, --file <file> load a specific file instead of utmp
-t, --until <YYYYMMDDHHMMSS> display the lines until the specified time
-s, --since <YYYYMMDDHHMMSS> display the lines since the specified time
-o, --output <json/tsv/csv> display the lines at specific format. Default format is json.
`, os.Args[0], os.Args[0])
}

// parse arguments
flag.StringVar(&filePath, "f", "", "file path")
flag.StringVar(&filePath, "file", "", "file path")
flag.StringVar(&until, "t", "", "until")
flag.StringVar(&until, "until", "", "until")
flag.StringVar(&since, "s", "", "since")
flag.StringVar(&since, "since", "", "since")
flag.StringVar(&outputType, "o", "", "output type")
flag.StringVar(&outputType, "output", "", "output type")
flag.Parse()
if filePath == "" {
flag.Usage()
os.Exit(1)
}
outputType = strings.ToLower(outputType)
if _, exist := printer.PrintTypes[outputType]; outputType != "" && !exist {
flag.Usage()
os.Exit(1)
}
sinceTime, _ := time.Parse("20060102150405", since)
untilTime, _ := time.Parse("20060102150405", until)

// read file
file, openErr := os.Open(filePath)
defer file.Close()
if openErr != nil {
fmt.Fprintln(os.Stderr, openErr)
os.Exit(1)
}
utmps, readErr := utmp.Read(file)
if readErr != nil {
fmt.Fprintln(os.Stderr, readErr)
os.Exit(1)
}

// to go utmp
var goUtmps []*utmp.GoUtmp
for _, gu := range utmps {
goUtmps = append(goUtmps, utmp.ToGoUtmp(gu))
}

// filter
filter := &filter.TimeFilter{}
goUtmps = filter.Filter(goUtmps, sinceTime, untilTime)

// print
printer := printer.GetPrinter(outputType)
for _, u := range goUtmps {
printer.Print(u)
}
}
28 changes: 28 additions & 0 deletions printer/csv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package printer

import (
"fmt"

"github.com/neko-neko/utmpdump/utmp"
)

// TSV printer
type CsvPrinter struct{}

// Format TSV
func (t *CsvPrinter) Print(u *utmp.GoUtmp) {
fmt.Printf(
"%d,%d,%s,%s,%s,%s,%d,%d,%d,%s,%s\n",
u.Type,
u.Pid,
u.Device,
u.Id,
u.User,
u.Host,
u.Exit.Exit,
u.Exit.Termination,
u.Session,
u.Time,
u.Addr,
)
}
17 changes: 17 additions & 0 deletions printer/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package printer

import (
"encoding/json"
"fmt"

"github.com/neko-neko/utmpdump/utmp"
)

// TSV printer
type JsonPrinter struct{}

// Format TSV
func (t *JsonPrinter) Print(u *utmp.GoUtmp) {
json, _ := json.Marshal(u)
fmt.Println(string(json))
}
36 changes: 36 additions & 0 deletions printer/printer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package printer

import (
"github.com/neko-neko/utmpdump/utmp"
)

const (
PrintTypeJson = "json"
PrintTypeCsv = "csv"
PrintTypeTsv = "tsv"
)

var PrintTypes = map[string]struct{}{
PrintTypeJson: struct{}{},
PrintTypeCsv: struct{}{},
PrintTypeTsv: struct{}{},
}

// Printer interface
type Printer interface {
Print(utmp *utmp.GoUtmp)
}

// Get formatter
func GetPrinter(formatType string) Printer {
switch formatType {
case PrintTypeJson:
return &JsonPrinter{}
case PrintTypeCsv:
return &CsvPrinter{}
case PrintTypeTsv:
return &TsvPrinter{}
default:
return &JsonPrinter{}
}
}
28 changes: 28 additions & 0 deletions printer/tsv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package printer

import (
"fmt"

"github.com/neko-neko/utmpdump/utmp"
)

// TSV printer
type TsvPrinter struct{}

// Format TSV
func (t *TsvPrinter) Print(u *utmp.GoUtmp) {
fmt.Printf(
"%d\t%d\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t%s\t%s\n",
u.Type,
u.Pid,
u.Device,
u.Id,
u.User,
u.Host,
u.Exit.Exit,
u.Exit.Termination,
u.Session,
u.Time,
u.Addr,
)
}
Loading

0 comments on commit 4b375b5

Please sign in to comment.