-
Notifications
You must be signed in to change notification settings - Fork 0
/
wincollector.go
141 lines (122 loc) · 3 KB
/
wincollector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"context"
"fmt"
"log"
"os"
"strings"
"time"
"cloud.google.com/go/firestore"
firebase "firebase.google.com/go"
wc "github.com/scjalliance/wincollector"
"google.golang.org/api/option"
)
// VERSION should be set via build flag
var VERSION = "unknown"
// Computer is a computer
type Computer struct {
Name string `firestore:"name,omitempty"`
Created time.Time `firestore:"created,omitempty"`
WCVersion string `firestore:"wcVersion,omitempty"`
WCLastRunStart time.Time `firestore:"wcLastRunStart,omitempty"`
WCLastRunEnd time.Time `firestore:"wcLastRunEnd,omitempty"`
WCLastError string `firestore:"wcLastError,omitempty"`
WCLastErrorTime time.Time `firestore:"wcLastErrorTime,omitempty"`
}
// Program is an installed program
type Program struct {
LastSeen time.Time `firestore:"lastSeen,omitempty"`
wc.Product
}
func main() {
verbose := len(os.Args) > 1 && os.Args[1] == "-v"
ctx := context.Background()
hostname, err := os.Hostname()
if err != nil {
log.Fatal(err)
}
hostname = strings.ToUpper(hostname)
app, err := firebase.NewApp(context.Background(), nil, option.WithCredentialsJSON(xSECRET))
if err != nil {
log.Fatal(err)
}
fs, err := app.Firestore(ctx)
if err != nil {
log.Fatal(err)
}
computerKey := "computers/" + hostname
programsKey := "installedSoftware"
computer := fs.Doc(computerKey)
_, err = computer.Create(ctx, Computer{
Created: time.Now(),
WCVersion: VERSION,
Name: hostname,
WCLastRunStart: time.Now(),
})
if err != nil {
computer.Update(ctx, []firestore.Update{
{
Path: "wcVersion",
Value: VERSION,
},
{
Path: "wcLastRunStart",
Value: time.Now(),
},
})
}
defer func() {
computer.Update(ctx, []firestore.Update{
{
Path: "wcLastRunEnd",
Value: time.Now(),
},
})
}()
ps, err := wc.GetProducts()
if err != nil {
computer.Update(ctx, []firestore.Update{
{
Path: "wcLastError",
Value: err.Error(),
},
{
Path: "wcLastErrorTime",
Value: time.Now(),
},
})
log.Println(err) // FIXME?
return
}
programsCollection := computer.Collection(programsKey)
for _, this := range ps {
if verbose {
fmt.Printf("[%s]\n\tBits: %d\n\tDisplayName: %s\n\tDisplayVersion: %s\n\tPublisher: %s\n\tComments: %s\n\tContact: %s\n\tInstallDate: %s\n\tInstallSource: %s\n\tInstallLocation: %s\n\tModifyPath: %s\n\tUninstallString: %s\n\tURLInfoAbout: %s\n\tURLUpdateInfo: %s\n\n",
this.Key,
this.Bits,
this.DisplayName,
this.DisplayVersion,
this.Publisher,
this.Comments,
this.Contact,
this.InstallDate,
this.InstallSource,
this.InstallLocation,
this.ModifyPath,
this.UninstallString,
this.URLInfoAbout,
this.URLUpdateInfo,
)
}
p := Program{
LastSeen: time.Now(),
Product: this,
}
programDoc := programsCollection.Doc(this.Key)
_, err = programDoc.Set(ctx, p)
if err != nil {
log.Println(err) // FIXME?
}
time.Sleep(time.Millisecond * 100)
}
}