1 /*
2  Uptime Client v5.0 beta
3 
4  $Id: stats-w2k.c,v 1.18 2002/12/22 17:53:42 carstenklapp Exp $
5 
6  Logs system uptime and statistics with Uptimes Project servers
7 
8  Copyright (C) 1999-2002 Martijn Broenland, Alex C. de Haas, Carsten Klapp
9 
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 
24  Carsten Klapp <carstenklapp@users.sourceforge.net>
25  Alex C. de Haas <alex@uptimes.net>
26  Martijn Broenland <tgm@uptimes.net>
27  */
28 
29 /**
30  * @filename    stats-w2k.c
31  *
32  * @desc        Retrieve stats for the Windows NT4/2000 platforms
33  */
34 
35 #if defined PLATFORM_WINNT
36 
37 /*@unused@*/ static const char rcsid[] =
38     "@(#)$Id: stats-w2k.c,v 1.18 2002/12/22 17:53:42 carstenklapp Exp $";
39 
40 /* My includes */
41 #include "upclient.h"
42 #include "options.h"
43 #include "stats.h"
44 
45 /* System includes */
46 #include <pdh.h>
47 #include <stdio.h>
48 #include <errno.h>
49 #include <string.h>
50 #include <windows.h>
51 
52 #if !defined SECONDS_PER_MINUTE
53 #   define SECONDS_PER_MINUTE 60;
54 #endif /* !defined SECONDS_PER_MINUTE */
55 
56 /**
57  * @desc    Verbose level 3 logging of calulations
58  */
59 void
logcalc(char * whatwascalculateddesc,char * value)60 logcalc(char *whatwascalculateddesc, char *value)
61 {
62 #if defined DEBUG
63     uplog(LOG_DEBUG, _("%s calculated: %s"), whatwascalculateddesc, value);
64 #endif /* DEBUG */
65 }
66 
67 /**
68  * @desc    Get statistics
69  */
70 void
getstats(unsigned long * uptimeminutes,double * UsagePercent,double * IdlePercent,char * os,char * osversion,char * cpu,double * loadavg)71 getstats(unsigned long *uptimeminutes, double *UsagePercent,
72          double *IdlePercent, char *os, char *osversion, char *cpu,
73          double *loadavg)
74 {
75     HQUERY myQuery;
76     HCOUNTER hPdhCounter;
77     PDH_STATUS nStatusCode;
78     PDH_FMT_COUNTERVALUE pdhfmtcv;
79     LONGLONG lUptime;
80     OSVERSIONINFO osVersionInfo;
81     SYSTEM_INFO si;
82     int    success = 0; /* false */
83 
84    /* open query to get perfomance data */
85     if (PdhOpenQuery(NULL, 0, &myQuery) == ERROR_SUCCESS) {
86        /* add system uptime to the query */
87         if (PdhAddCounter
88             (myQuery, "\\\\.\\System\\System Up Time", 0,
89              &hPdhCounter) == ERROR_SUCCESS) {
90            /* execute query */
91             if (PdhCollectQueryData(myQuery) == ERROR_SUCCESS) {
92                 nStatusCode =
93                     PdhGetFormattedCounterValue(hPdhCounter, PDH_FMT_LARGE,
94                                                 NULL, &pdhfmtcv);
95 
96                 if (nStatusCode == ERROR_SUCCESS && pdhfmtcv.CStatus == 0) {
97                     lUptime = pdhfmtcv.largeValue;
98                     lUptime /= SECONDS_PER_MINUTE;      /* convert to minutes; */
99 
100                    /* convert 64-bits value -> 32 bits value... inaccurate when
101                       system longer up than 136 years */
102                     *uptimeminutes = (unsigned long)lUptime;
103 
104                     success = 1;
105                 }
106             }
107         }
108        /* close query */
109         PdhCloseQuery(myQuery);
110     }
111 
112    /* when failing... which should *never* happen return 0 for uptime */
113     if (!success) {
114         *uptimeminutes = 0;
115     }
116 
117     if (cfg_sendosname) {
118        /* this *must* be windows if its built against PLATFORM_WINNT */
119         strcpy(osname, "Windows");
120         logcalc(_("OS"), osname);
121     }
122 
123     if (cfg_sendosversion) {
124        /* retrieve os version */
125         osVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
126         GetVersionEx(&osVersionInfo);
127 
128         switch (osVersionInfo.dwMajorVersion) {
129             case 5:
130                 strcpy(osversion, "2000");
131                 break;
132             case 4:
133                 if (osVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
134                     strcpy(osversion, "NT");
135                 }
136                 else {
137                     strcpy(osversion,
138                            (osVersionInfo.dwMinorVersion == 0 ? "95" : "98"));
139                 }
140                 break;
141             case 3:
142                 if (osVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
143                     strcpy(osversion, "NT 3.x");
144                 }
145                 else {
146                    /* This should only happen if Win32s is installed, should
147                       never happen */
148                     strcpy(osversion, "3.x");
149                 }
150                 break;
151             default:
152                 strcpy(osversion, "unknown");
153         }
154         logcalc(_("OS version"), osversion);
155     }
156 
157     if (cfg_sendcpu || cfg_sendcpudetail) {
158        /* retrieve system info */
159         GetSystemInfo(&si);
160         if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_ALPHA) {
161             strcpy(cpu, "alpha");
162         }
163         else {
164             if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL) {
165                 sprintf(cpu, "i%d86", si.wProcessorLevel);
166             }
167             else {
168                 strcpy(cpu, "unknown");
169             }
170         }
171         logcalc(_("CPU"), cpu);
172     }
173 }
174 #endif /* PLATFORM_WINNT */
175