-
Notifications
You must be signed in to change notification settings - Fork 0
/
SystemManager.cs
189 lines (142 loc) · 5.61 KB
/
SystemManager.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Xml.Linq;
using System.ComponentModel;
namespace Systerminator
{
public class GroupItem : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string Title {get;set;}
public List<HostDetails> Hosts {get;set;}
public bool IsSelected { get; set; }
}
public class HostDetails : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string HostName { get; set; }
public string Status { get; set; }
public DateTime? LastPinged { get;set;}
public bool AllowShutdown { get; set; }
}
public class SystemManager : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public List<GroupItem> Groups { get; set; }
public GroupItem SelectedGroup
{
get {
return Groups.FirstOrDefault(g => g.IsSelected == true);
}
set
{
foreach (GroupItem g in Groups)
{
if (g == value) g.IsSelected = true;
else g.IsSelected = false;
}
}
}
public void LoadHosts()
{
Groups = new List<GroupItem>();
XDocument xdoc = XDocument.Load("HostGroup.xml");
var groups = xdoc.Elements("groups");
foreach(XElement g in groups.Elements("group"))
{
GroupItem grp = new GroupItem();
grp.Title = g.Attribute("title").Value;
grp.Hosts = new List<HostDetails>();
var items = g.Elements("item");
foreach (var val in items)
{
HostDetails host = new HostDetails();
host.HostName = val.Value;
host.AllowShutdown = val.Attribute("allowshutdown") != null ? bool.Parse(val.Attribute("allowshutdown").Value) : true;
grp.Hosts.Add(host);
}
Groups.Add(grp);
}
}
public void PerformBatchPing(GroupItem group)
{
foreach (var host in group.Hosts)
{
Ping p = new Ping();
p.PingCompleted += new PingCompletedEventHandler(PerformPingCompleted);
p.SendAsync(host.HostName, host);
}
}
void PerformPingCompleted(object sender, PingCompletedEventArgs e)
{
var reply = e.Reply;
HostDetails host = (HostDetails)e.UserState;
string statusText = "No Response";
if (reply != null)
{
statusText = reply.Status.ToString();
}
foreach (var group in Groups)
{
if (group.Hosts.Contains(host))
{
foreach (var h in group.Hosts.Where(ht => ht == host))
{
h.Status = statusText;
h.LastPinged = DateTime.Now;
}
}
}
}
public void PerformSystemCommand(string command)
{
System.Diagnostics.Debug.WriteLine(command);
ExecuteCommandSync(command);
}
public void ExecuteCommandSync(object command)
{
try
{
// create the ProcessStartInfo using "cmd" as the program to be run, and "/c " as the parameters.
// Incidentally, /c tells cmd that we want it to execute the command that follows, and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
// The following commands are needed to redirect the standard output.
//This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
// Display the command output.
Console.WriteLine(result);
}
catch (Exception)
{
// Log the exception
}
}
public void PerformBatchShutdown(GroupItem g, string CommandText)
{
foreach (var host in g.Hosts)
{
if (host.AllowShutdown)
{
string command = CommandText.Replace("{MachineName}", host.HostName);
PerformSystemCommand(command);
}
else
{
System.Diagnostics.Debug.WriteLine("Shutdown not permitted for host:"+host.HostName);
}
}
}
}
}