-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
241 lines (194 loc) · 6.55 KB
/
main.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"bufio"
"fmt"
"log"
"os"
"sort"
"sync"
"time"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/mgr"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/urfave/cli"
)
var authors = []*cli.Author{
{
Name: "Dixon Begay",
},
}
// Path to database (sqlite)
var dbPath string
// File path containing list of computers
var filePath string
// Database file
var db *gorm.DB
func openFileAndCheck() {
db, err := gorm.Open("sqlite3", dbPath)
if err != nil {
panic("failed to connect database")
}
db.AutoMigrate(&Computer{}, &VcError{})
defer db.Close()
// Create the waitgroup that will count number of goroutines
var wg sync.WaitGroup
// Open the file that was passed as an argument to the program
file, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
// Setup the reader for the file that was passed as an argument to the program
scanner := bufio.NewScanner(file)
log.Println("------------ Starting to check if vuecentric service is running on computers ------------")
// Start reading file line by line
for scanner.Scan() {
go func(computerName string) {
// Add 1 to the waitgroup for this goroutine
wg.Add(1)
//Connect to remote computer
computer, err := mgr.ConnectRemote(computerName)
if err != nil {
log.Printf("[%s] %v", computerName, err)
} else {
// Get the lock status of the service controller
lockStatus, err := computer.LockStatus()
if err != nil {
log.Printf("[%s] %v", computerName, err)
}
// Check if the service controller is locked by, and if so, by who, and for how long
if lockStatus.IsLocked {
log.Println(fmt.Sprintf("[%s] Locked by %s for %v", computerName, lockStatus.Owner, lockStatus.Age))
} else {
// Get the vuecentric service
vcUpdaterSvc, err := computer.OpenService("vcUpdater")
if err != nil {
log.Printf("[%s] %v", computerName, err)
} else {
// Delcare svcStatus here so we are not making declarations every loop
var svcStatus svc.Status
svcStatus.State = svc.State(0)
// Error counter for keeping track of how many attempts are made to query the vcUpdater service
errCount := 0
// Keep looping until the service is running
queryLoop:
for {
// Get the vcUpdater service
svcStatus, err = vcUpdaterSvc.Query()
if err != nil {
log.Printf("[%s] %v", computerName, err)
errCount++
if errCount > 2 {
log.Printf("[%s] Failed to query vcUpdater service after %v attempts\n", computerName, errCount)
break queryLoop
}
} else {
switch svcStatus.State {
case svc.Stopped:
log.Printf("[%s] vcUpdater is stopped\n", computerName)
// If the service is stopped then start the vcUpdater service
if err = vcUpdaterSvc.Start(); err != nil {
log.Printf("[%s] %v", computerName, err)
break queryLoop
}
computer := Computer{Name: computerName}
db.Where(Computer{Name: computerName}).FirstOrCreate(&computer)
vcError := VcError{
Computer: computerName,
Status: "stopped",
}
db.Save(computer)
db.Model(&computer).Association("VcErrors").Append(vcError)
db.Save(computer)
db.Save(vcError)
log.Printf("[%s] starting vcUpdater\n", computerName)
case svc.StartPending:
log.Printf("[%s] vcUpdater is starting up\n", computerName)
case svc.StopPending:
log.Printf("[%s] vcUpdater is stopping\n", computerName)
case svc.Running:
log.Printf("[%s] vcUpdater is running\n", computerName)
break queryLoop
case svc.ContinuePending:
log.Printf("[%s] vcUpdater is starting up\n", computerName)
case svc.PausePending:
log.Printf("[%s] vcUpdater is pausing\n", computerName)
case svc.Paused:
log.Printf("[%s] vcUpdater is paused\n", computerName)
_, err = vcUpdaterSvc.Control(svc.Continue)
if err != nil {
log.Printf("[%s] %v", computerName, err)
}
computer := Computer{Name: computerName}
db.Where(Computer{Name: computerName}).FirstOrCreate(&computer)
vcError := VcError{
Computer: computerName,
Status: "paused",
}
db.Save(computer)
db.Model(&computer).Association("VcErrors").Append(vcError)
db.Save(computer)
db.Save(vcError)
log.Printf("[%s] resuming vcUpdater\n", computerName)
}
}
// Wait 2 seconds so computer and network isn't spammed with requests
time.Sleep(time.Second * 1)
}
}
}
// Disconnect from the computer
computer.Disconnect()
}
// Reduce the counter for the waitgroup
wg.Done()
}(scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
// Allow program to run through all goroutines before exiting
wg.Wait()
log.Println("------------ Finished checking computers ------------")
}
func main() {
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "dbPath",
Aliases: []string{"db"},
Usage: "Path to database file (sqlite3). If no path is specified, memory will be used.",
Value: ":memory:",
TakesFile: true,
Destination: &dbPath,
},
&cli.StringFlag{
Name: "computerList",
Usage: "Path to file containing list of remote computers on a network.",
TakesFile: true,
Destination: &filePath,
Required: true,
},
},
Name: "vuecentric-checker",
Usage: "vuecentric-checker ensures VueCentric service is running for a list of Windows computers.",
UsageText: "vuecentric-checker ensures VueCentric service is running for a list of Windows computers.",
ArgsUsage: "ListOfComputers (file containing a list of remote computers on a network)",
Copyright: "Copyright (c) 2020 Dixon Begay aka shadez95",
Authors: authors,
Action: func(c *cli.Context) error {
// Check if flags are present first. If not, continue and throw up helper info
if c.NumFlags() > 0 {
openFileAndCheck()
}
return nil
},
}
sort.Sort(cli.FlagsByName(app.Flags))
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
os.Exit(1)
}
os.Exit(0)
}