1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS net command
4  * FILE:            base/applications/network/net/cmdStatistics.c
5  * PROGRAMMERS:     Eric Kohl <eric.kohl@reactos.org>
6  */
7 
8 #include "net.h"
9 #include <rtltypes.h>
10 #include <rtlfuncs.h>
11 
12 static
13 INT
14 DisplayServerStatistics(VOID)
15 {
16     PSERVER_INFO_100 ServerInfo = NULL;
17     PSTAT_SERVER_0 StatisticsInfo = NULL;
18     LARGE_INTEGER LargeValue;
19     FILETIME FileTime, LocalFileTime;
20     SYSTEMTIME SystemTime;
21     WORD wHour;
22     INT nPaddedLength = 35;
23     NET_API_STATUS Status;
24 
25     Status = NetServerGetInfo(NULL, 100, (PBYTE*)&ServerInfo);
26     if (Status != NERR_Success)
27         goto done;
28 
29     Status = NetStatisticsGet(NULL,
30                               SERVICE_SERVER,
31                               0,
32                               0,
33                               (LPBYTE*)&StatisticsInfo);
34     if (Status != NERR_Success)
35         goto done;
36 
37     ConResPrintf(StdOut, IDS_STATISTICS_SRV_NAME, ServerInfo->sv100_name);
38 
39     RtlSecondsSince1970ToTime(StatisticsInfo->sts0_start,
40                               &LargeValue);
41     FileTime.dwLowDateTime = LargeValue.u.LowPart;
42     FileTime.dwHighDateTime = LargeValue.u.HighPart;
43     FileTimeToLocalFileTime(&FileTime, &LocalFileTime);
44     FileTimeToSystemTime(&LocalFileTime, &SystemTime);
45 
46     wHour = SystemTime.wHour;
47     if (wHour == 0)
48     {
49         wHour = 12;
50     }
51     else if (wHour > 12)
52     {
53         wHour = wHour - 12;
54     }
55 
56     ConResPrintf(StdOut, IDS_STATISTICS_SINCE,
57                  SystemTime.wMonth, SystemTime.wDay, SystemTime.wYear,
58                  wHour, SystemTime.wMinute,
59                  (SystemTime.wHour >= 1 && SystemTime.wHour < 13) ? L"AM" : L"PM");
60 
61     PrintPaddedResourceString(IDS_STATISTICS_SRV_SESACCEPT, nPaddedLength);
62     ConPrintf(StdOut, L"%lu\n", StatisticsInfo->sts0_sopens);
63 
64     PrintPaddedResourceString(IDS_STATISTICS_SRV_SESSTIME, nPaddedLength);
65     ConPrintf(StdOut, L"%lu\n", StatisticsInfo->sts0_stimedout);
66 
67     PrintPaddedResourceString(IDS_STATISTICS_SRV_SESSERROR, nPaddedLength);
68     ConPrintf(StdOut, L"%lu\n\n", StatisticsInfo->sts0_serrorout);
69 
70     LargeValue.u.LowPart = StatisticsInfo->sts0_bytessent_low;
71     LargeValue.u.HighPart = StatisticsInfo->sts0_bytessent_high;
72     PrintPaddedResourceString(IDS_STATISTICS_SRV_KBSENT, nPaddedLength);
73     ConPrintf(StdOut, L"%I64u\n", LargeValue.QuadPart / 1024);
74 
75     LargeValue.u.LowPart = StatisticsInfo->sts0_bytesrcvd_low;
76     LargeValue.u.HighPart = StatisticsInfo->sts0_bytesrcvd_high;
77     PrintPaddedResourceString(IDS_STATISTICS_SRV_KBRCVD, nPaddedLength);
78     ConPrintf(StdOut, L"%I64u\n", LargeValue.QuadPart / 1024);
79 
80     PrintPaddedResourceString(IDS_STATISTICS_SRV_MRESPTIME, nPaddedLength);
81     ConPrintf(StdOut, L"%lu\n\n", StatisticsInfo->sts0_avresponse);
82 
83     PrintPaddedResourceString(IDS_STATISTICS_SRV_SYSERRORS, nPaddedLength);
84     ConPrintf(StdOut, L"%lu\n", StatisticsInfo->sts0_syserrors);
85 
86     PrintPaddedResourceString(IDS_STATISTICS_SRV_PMERRORS, nPaddedLength);
87     ConPrintf(StdOut, L"%lu\n", StatisticsInfo->sts0_permerrors);
88 
89     PrintPaddedResourceString(IDS_STATISTICS_SRV_PWERRORS, nPaddedLength);
90     ConPrintf(StdOut, L"%lu\n\n", StatisticsInfo->sts0_pwerrors);
91 
92     PrintPaddedResourceString(IDS_STATISTICS_SRV_FILES, nPaddedLength);
93     ConPrintf(StdOut, L"%lu\n", StatisticsInfo->sts0_fopens);
94 
95     PrintPaddedResourceString(IDS_STATISTICS_SRV_DEVICES, nPaddedLength);
96     ConPrintf(StdOut, L"%lu\n", StatisticsInfo->sts0_devopens);
97 
98     PrintPaddedResourceString(IDS_STATISTICS_SRV_JOBS, nPaddedLength);
99     ConPrintf(StdOut, L"%lu\n\n", StatisticsInfo->sts0_jobsqueued);
100 
101     ConResPuts(StdOut, IDS_STATISTICS_SRV_BUFFERS);
102 
103     PrintPaddedResourceString(IDS_STATISTICS_SRV_BIGBUFFERS, nPaddedLength);
104     ConPrintf(StdOut, L"%lu\n", StatisticsInfo->sts0_bigbufneed);
105 
106     PrintPaddedResourceString(IDS_STATISTICS_SRV_REQBUFFERS, nPaddedLength);
107     ConPrintf(StdOut, L"%lu\n\n", StatisticsInfo->sts0_reqbufneed);
108 
109 done:
110     if (StatisticsInfo != NULL)
111         NetApiBufferFree(StatisticsInfo);
112 
113     if (ServerInfo != NULL)
114         NetApiBufferFree(ServerInfo);
115 
116     return 0;
117 }
118 
119 
120 static
121 INT
122 DisplayWorkstationStatistics(VOID)
123 {
124     PWKSTA_INFO_100 WorkstationInfo = NULL;
125     PSTAT_WORKSTATION_0 StatisticsInfo = NULL;
126     LARGE_INTEGER LargeValue;
127     FILETIME FileTime, LocalFileTime;
128     SYSTEMTIME SystemTime;
129     WORD wHour;
130     INT nPaddedLength = 47;
131     NET_API_STATUS Status;
132 
133     Status = NetWkstaGetInfo(NULL,
134                              100,
135                              (PBYTE*)&WorkstationInfo);
136     if (Status != NERR_Success)
137         goto done;
138 
139     Status = NetStatisticsGet(NULL,
140                               SERVICE_SERVER,
141                               0,
142                               0,
143                               (LPBYTE*)&StatisticsInfo);
144     if (Status != NERR_Success)
145         goto done;
146 
147     ConResPrintf(StdOut, IDS_STATISTICS_WKS_NAME, WorkstationInfo->wki100_computername);
148 
149     RtlSecondsSince1970ToTime(StatisticsInfo->StatisticsStartTime.u.LowPart,
150                               &LargeValue);
151     FileTime.dwLowDateTime = LargeValue.u.LowPart;
152     FileTime.dwHighDateTime = LargeValue.u.HighPart;
153     FileTimeToLocalFileTime(&FileTime, &LocalFileTime);
154     FileTimeToSystemTime(&LocalFileTime, &SystemTime);
155 
156     wHour = SystemTime.wHour;
157     if (wHour == 0)
158     {
159         wHour = 12;
160     }
161     else if (wHour > 12)
162     {
163         wHour = wHour - 12;
164     }
165 
166     ConResPrintf(StdOut, IDS_STATISTICS_SINCE,
167                  SystemTime.wMonth, SystemTime.wDay, SystemTime.wYear,
168                  wHour, SystemTime.wMinute, (SystemTime.wHour >= 1 && SystemTime.wHour < 13) ? L"AM" : L"PM");
169 
170     PrintPaddedResourceString(IDS_STATISTICS_WKS_BYTESRCVD, nPaddedLength);
171     ConPrintf(StdOut, L"%I64u\n", StatisticsInfo->BytesReceived.QuadPart);
172 
173     PrintPaddedResourceString(IDS_STATISTICS_WKS_SMBSRCVD, nPaddedLength);
174     ConPrintf(StdOut, L"%I64u\n", StatisticsInfo->SmbsReceived.QuadPart);
175 
176     PrintPaddedResourceString(IDS_STATISTICS_WKS_BYTESTRANS, nPaddedLength);
177     ConPrintf(StdOut, L"%I64u\n", StatisticsInfo->BytesTransmitted.QuadPart);
178 
179     PrintPaddedResourceString(IDS_STATISTICS_WKS_SMBSTRANS, nPaddedLength);
180     ConPrintf(StdOut, L"%I64u\n", StatisticsInfo->SmbsTransmitted.QuadPart);
181 
182     PrintPaddedResourceString(IDS_STATISTICS_WKS_READOPS, nPaddedLength);
183     ConPrintf(StdOut, L"%lu\n", StatisticsInfo->ReadOperations);
184 
185     PrintPaddedResourceString(IDS_STATISTICS_WKS_WRITEOPS, nPaddedLength);
186     ConPrintf(StdOut, L"%lu\n", StatisticsInfo->WriteOperations);
187 
188     PrintPaddedResourceString(IDS_STATISTICS_WKS_READDENIED, nPaddedLength);
189     ConPrintf(StdOut, L"%lu\n", StatisticsInfo->RawReadsDenied);
190 
191     PrintPaddedResourceString(IDS_STATISTICS_WKS_WRITEDENIED, nPaddedLength);
192     ConPrintf(StdOut, L"%lu\n\n", StatisticsInfo->RawWritesDenied);
193 
194     PrintPaddedResourceString(IDS_STATISTICS_WKS_NETWORKERROR, nPaddedLength);
195     ConPrintf(StdOut, L"%lu\n", StatisticsInfo->NetworkErrors);
196 
197     PrintPaddedResourceString(IDS_STATISTICS_WKS_CONNECTS, nPaddedLength);
198     ConPrintf(StdOut, L"%lu\n", StatisticsInfo->CoreConnects +
199                                 StatisticsInfo->Lanman20Connects +
200                                 StatisticsInfo->Lanman21Connects +
201                                 StatisticsInfo->LanmanNtConnects);
202 
203     PrintPaddedResourceString(IDS_STATISTICS_WKS_RECONNECTS, nPaddedLength);
204     ConPrintf(StdOut, L"%lu\n", StatisticsInfo->Reconnects);
205 
206     PrintPaddedResourceString(IDS_STATISTICS_WKS_DISCONNECTS, nPaddedLength);
207     ConPrintf(StdOut, L"%lu\n\n", StatisticsInfo->ServerDisconnects);
208 
209     PrintPaddedResourceString(IDS_STATISTICS_WKS_SESSIONS, nPaddedLength);
210     ConPrintf(StdOut, L"%lu\n", StatisticsInfo->Sessions);
211 
212     PrintPaddedResourceString(IDS_STATISTICS_WKS_HUNGSESSIONS, nPaddedLength);
213     ConPrintf(StdOut, L"%lu\n", StatisticsInfo->HungSessions);
214 
215     PrintPaddedResourceString(IDS_STATISTICS_WKS_FAILSESSIONS, nPaddedLength);
216     ConPrintf(StdOut, L"%lu\n", StatisticsInfo->FailedSessions);
217 
218     PrintPaddedResourceString(IDS_STATISTICS_WKS_FAILEDOPS, nPaddedLength);
219     ConPrintf(StdOut, L"%lu\n", StatisticsInfo->InitiallyFailedOperations +
220                                 StatisticsInfo->FailedCompletionOperations);
221 
222     PrintPaddedResourceString(IDS_STATISTICS_WKS_USECOUNT, nPaddedLength);
223     ConPrintf(StdOut, L"%lu\n", StatisticsInfo->UseCount);
224 
225     PrintPaddedResourceString(IDS_STATISTICS_WKS_FAILUSECOUNT, nPaddedLength);
226     ConPrintf(StdOut, L"%lu\n\n", StatisticsInfo->FailedUseCount);
227 
228 done:
229     if (StatisticsInfo != NULL)
230         NetApiBufferFree(StatisticsInfo);
231 
232     if (WorkstationInfo != NULL)
233         NetApiBufferFree(WorkstationInfo);
234 
235     return 0;
236 }
237 
238 
239 INT
240 cmdStatistics(
241     INT argc,
242     WCHAR **argv)
243 {
244     INT i, result = 0;
245     BOOL bServer = FALSE;
246     BOOL bWorkstation = FALSE;
247 
248     for (i = 2; i < argc; i++)
249     {
250         if (_wcsicmp(argv[i], L"server") == 0)
251         {
252             if (bWorkstation == FALSE)
253                 bServer = TRUE;
254             continue;
255         }
256 
257         if (_wcsicmp(argv[i], L"workstation") == 0)
258         {
259             if (bServer == FALSE)
260                 bWorkstation = TRUE;
261             continue;
262         }
263 
264         if (_wcsicmp(argv[i], L"help") == 0)
265         {
266             /* Print short syntax help */
267             ConResPuts(StdOut, IDS_GENERIC_SYNTAX);
268             ConResPuts(StdOut, IDS_STATISTICS_SYNTAX);
269             return 0;
270         }
271 
272         if (_wcsicmp(argv[i], L"/help") == 0)
273         {
274             /* Print full help text*/
275             ConResPuts(StdOut, IDS_GENERIC_SYNTAX);
276             ConResPuts(StdOut, IDS_STATISTICS_SYNTAX);
277             ConResPuts(StdOut, IDS_STATISTICS_HELP_1);
278             ConResPuts(StdOut, IDS_STATISTICS_HELP_2);
279             return 0;
280         }
281     }
282 
283     if (bServer)
284     {
285         result = DisplayServerStatistics();
286     }
287     else if (bWorkstation)
288     {
289         result = DisplayWorkstationStatistics();
290     }
291     else
292     {
293         ConResPuts(StdOut, IDS_STATISTICS_TEXT);
294     }
295 
296     if (result == 0)
297         ConResPuts(StdOut, IDS_ERROR_NO_ERROR);
298 
299     return result;
300 }