2014-11-06 16:36:42 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2014-11-06 16:36:42 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
2014-11-06 15:17:08 +01:00
|
|
|
#include <set>
|
|
|
|
#include <Shlwapi.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <math.h>
|
2015-06-23 13:15:55 +02:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/bind.hpp>
|
2014-11-06 15:17:08 +01:00
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
#include "check_disk.h"
|
2014-11-06 15:17:08 +01:00
|
|
|
|
2015-01-23 16:34:16 +01:00
|
|
|
#define VERSION 1.1
|
2014-11-06 15:17:08 +01:00
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
static BOOL debug = FALSE;
|
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
INT wmain(INT argc, WCHAR **argv)
|
2014-11-06 15:17:08 +01:00
|
|
|
{
|
2015-03-23 13:07:02 +01:00
|
|
|
std::vector<drive> vDrives;
|
2014-12-19 10:21:38 +01:00
|
|
|
printInfoStruct printInfo{ };
|
2014-11-06 15:17:08 +01:00
|
|
|
po::variables_map vm;
|
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
INT ret;
|
2014-11-06 15:17:08 +01:00
|
|
|
|
|
|
|
ret = parseArguments(argc, argv, vm, printInfo);
|
|
|
|
if (ret != -1)
|
|
|
|
return ret;
|
|
|
|
|
2014-12-19 10:21:38 +01:00
|
|
|
printInfo.warn.legal = !printInfo.warn.legal;
|
|
|
|
printInfo.crit.legal = !printInfo.crit.legal;
|
|
|
|
|
2014-11-06 15:17:08 +01:00
|
|
|
if (printInfo.drives.empty())
|
2015-06-23 13:15:55 +02:00
|
|
|
ret = check_drives(vDrives, printInfo.exclude_drives);
|
2014-11-06 15:17:08 +01:00
|
|
|
else
|
|
|
|
ret = check_drives(vDrives, printInfo);
|
|
|
|
|
|
|
|
if (ret != -1)
|
|
|
|
return ret;
|
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
for (std::vector<drive>::iterator it = vDrives.begin(); it != vDrives.end(); ++it) {
|
2014-12-19 10:21:38 +01:00
|
|
|
if (!getFreeAndCap(*it, printInfo.unit)) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << "Failed to access drive at " << it->name << '\n';
|
2014-11-06 15:17:08 +01:00
|
|
|
return 3;
|
2014-12-19 10:21:38 +01:00
|
|
|
}
|
2014-11-06 15:17:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return printOutput(printInfo, vDrives);
|
|
|
|
}
|
|
|
|
|
2015-06-23 13:15:55 +02:00
|
|
|
static INT parseArguments(INT ac, WCHAR **av, po::variables_map& vm, printInfoStruct& printInfo)
|
2014-11-06 15:17:08 +01:00
|
|
|
{
|
2015-03-23 13:07:02 +01:00
|
|
|
WCHAR namePath[MAX_PATH];
|
2014-11-06 15:17:08 +01:00
|
|
|
GetModuleFileName(NULL, namePath, MAX_PATH);
|
2015-03-23 13:07:02 +01:00
|
|
|
WCHAR *progName = PathFindFileName(namePath);
|
2014-11-06 15:17:08 +01:00
|
|
|
|
|
|
|
po::options_description desc("Options");
|
|
|
|
|
|
|
|
desc.add_options()
|
2015-03-23 13:07:02 +01:00
|
|
|
("help,h", "Print usage message and exit")
|
|
|
|
("version,V", "Print version and exit")
|
2015-01-12 15:53:25 +01:00
|
|
|
("debug,d", "Verbose/Debug output")
|
2015-03-23 13:07:02 +01:00
|
|
|
("warning,w", po::wvalue<std::wstring>(), "Warning threshold")
|
|
|
|
("critical,c", po::wvalue<std::wstring>(), "Critical threshold")
|
|
|
|
("path,p", po::wvalue<std::vector<std::wstring>>()->multitoken(), "Declare explicitly which drives to check (default checks all)")
|
2015-06-23 13:15:55 +02:00
|
|
|
("exclude_device,x", po::wvalue<std::vector<std::wstring>>()->multitoken(), "Exclude these drives from check")
|
|
|
|
("exclude-type,X", po::wvalue<std::vector<std::wstring>>()->multitoken(), "Exclude partition types (ignored)")
|
2015-08-15 10:26:43 +02:00
|
|
|
("iwarning,W", po::wvalue<std::wstring>(), "Warning threshold for inodes (ignored)")
|
|
|
|
("icritical,K", po::wvalue<std::wstring>(), "Critical threshold for inodes (ignored)")
|
2015-06-23 13:15:55 +02:00
|
|
|
("unit,u", po::wvalue<std::wstring>(), "Assign unit possible are: B, kB, MB, GB, TB")\
|
|
|
|
("megabytes,m", "use megabytes, overridden by -unit")
|
2014-11-06 15:17:08 +01:00
|
|
|
;
|
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
po::basic_command_line_parser<WCHAR> parser(ac, av);
|
2014-11-06 15:17:08 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
po::store(
|
|
|
|
parser
|
2015-06-23 13:15:55 +02:00
|
|
|
.options(desc)
|
2014-11-06 15:17:08 +01:00
|
|
|
.style(
|
|
|
|
po::command_line_style::unix_style |
|
|
|
|
po::command_line_style::allow_long_disguise)
|
|
|
|
.run(),
|
|
|
|
vm);
|
|
|
|
vm.notify();
|
2014-11-06 16:36:42 +01:00
|
|
|
} catch (std::exception& e) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << e.what() << '\n' << desc << '\n';
|
2014-11-06 15:17:08 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vm.count("help")) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << progName << " Help\n\tVersion: " << VERSION << '\n';
|
2014-11-06 15:17:08 +01:00
|
|
|
wprintf(
|
|
|
|
L"%s is a simple program to check a machines free disk space.\n"
|
|
|
|
L"You can use the following options to define its behaviour:\n\n", progName);
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << desc;
|
2014-11-06 15:17:08 +01:00
|
|
|
wprintf(
|
|
|
|
L"\nIt will then output a string looking something like this:\n\n"
|
2015-03-06 12:14:09 +01:00
|
|
|
L"\tDISK WARNING 29GB | disk=29GB;50%%;5;0;120\n\n"
|
2014-11-06 15:17:08 +01:00
|
|
|
L"\"DISK\" being the type of the check, \"WARNING\" the returned status\n"
|
|
|
|
L"and \"23.8304%%\" is the returned value.\n"
|
|
|
|
L"The performance data is found behind the \"|\", in order:\n"
|
|
|
|
L"returned value, warning threshold, critical threshold, minimal value and,\n"
|
2014-12-19 10:21:38 +01:00
|
|
|
L"if applicable, the maximal value.\n"
|
2014-11-06 15:17:08 +01:00
|
|
|
L"This program will also print out additional performance data disk by disk\n\n"
|
2014-11-06 16:36:42 +01:00
|
|
|
L"%s' exit codes denote the following:\n\n"
|
|
|
|
L" 0\tOK,\n\tNo Thresholds were broken or the programs check part was not executed\n"
|
2014-11-06 15:17:08 +01:00
|
|
|
L" 1\tWARNING,\n\tThe warning, but not the critical threshold was broken\n"
|
|
|
|
L" 2\tCRITICAL,\n\tThe critical threshold was broken\n"
|
2015-06-23 13:15:55 +02:00
|
|
|
L" 3\tUNKNOWN, \n\tThe program experienced an internal or input error\n\n"
|
2014-11-06 15:17:08 +01:00
|
|
|
L"Threshold syntax:\n\n"
|
|
|
|
L"-w THRESHOLD\n"
|
2014-12-19 10:21:38 +01:00
|
|
|
L"warn if threshold is broken, which means VALUE < THRESHOLD\n\n"
|
2014-11-06 15:17:08 +01:00
|
|
|
L"-w !THRESHOLD\n"
|
2014-12-19 10:21:38 +01:00
|
|
|
L"inverts threshold check, VALUE > THRESHOLD (analogous to above)\n\n"
|
2014-11-06 15:17:08 +01:00
|
|
|
L"-w [THR1-THR2]\n"
|
|
|
|
L"warn is VALUE is inside the range spanned by THR1 and THR2\n\n"
|
|
|
|
L"-w ![THR1-THR2]\n"
|
|
|
|
L"warn if VALUE is outside the range spanned by THR1 and THR2\n\n"
|
|
|
|
L"-w THRESHOLD%%\n"
|
|
|
|
L"if the plugin accepts percentage based thresholds those will be used.\n"
|
|
|
|
L"Does nothing if the plugin does not accept percentages, or only uses\n"
|
|
|
|
L"percentage thresholds. Ranges can be used with \"%%\", but both range values need\n"
|
|
|
|
L"to end with a percentage sign.\n\n"
|
|
|
|
L"All of these options work with the critical threshold \"-c\" too."
|
|
|
|
, progName);
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << '\n';
|
2014-11-06 15:17:08 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vm.count("version"))
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << "Version: " << VERSION << '\n';
|
2014-11-06 15:17:08 +01:00
|
|
|
|
2014-11-10 14:39:12 +01:00
|
|
|
if (vm.count("warning")) {
|
|
|
|
try {
|
2015-03-23 13:07:02 +01:00
|
|
|
printInfo.warn = threshold(vm["warning"].as<std::wstring>());
|
2014-11-10 14:39:12 +01:00
|
|
|
} catch (std::invalid_argument& e) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << e.what() << '\n';
|
2014-11-10 14:39:12 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (vm.count("critical")) {
|
|
|
|
try {
|
2015-03-23 13:07:02 +01:00
|
|
|
printInfo.crit = threshold(vm["critical"].as<std::wstring>());
|
2014-11-10 14:39:12 +01:00
|
|
|
} catch (std::invalid_argument& e) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::cout << e.what() << '\n';
|
2014-11-10 14:39:12 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}
|
2014-11-06 15:17:08 +01:00
|
|
|
|
2014-11-19 06:40:58 +01:00
|
|
|
if (vm.count("path"))
|
2015-03-23 13:07:02 +01:00
|
|
|
printInfo.drives = vm["path"].as<std::vector<std::wstring>>();
|
2014-11-06 15:17:08 +01:00
|
|
|
|
2015-06-23 13:15:55 +02:00
|
|
|
if (vm.count("exclude_device"))
|
|
|
|
printInfo.exclude_drives = vm["exclude_device"].as<std::vector<std::wstring>>();
|
|
|
|
|
2014-11-06 16:36:42 +01:00
|
|
|
if (vm.count("unit")) {
|
|
|
|
try {
|
2015-03-23 13:07:02 +01:00
|
|
|
printInfo.unit = parseBUnit(vm["unit"].as<std::wstring>());
|
2014-11-06 16:36:42 +01:00
|
|
|
} catch (std::invalid_argument) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << "Unknown unit Type " << vm["unit"].as<std::wstring>() << '\n';
|
2014-11-06 16:36:42 +01:00
|
|
|
return 3;
|
|
|
|
}
|
2015-06-23 13:15:55 +02:00
|
|
|
} else {
|
|
|
|
if (vm.count("megabytes"))
|
|
|
|
printInfo.unit = BunitMB;
|
|
|
|
else
|
|
|
|
printInfo.unit = BunitB;
|
|
|
|
}
|
2014-11-06 16:36:42 +01:00
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
if (vm.count("debug"))
|
|
|
|
debug = TRUE;
|
|
|
|
|
2014-11-06 15:17:08 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 13:15:55 +02:00
|
|
|
static INT printOutput(printInfoStruct& printInfo, std::vector<drive>& vDrives)
|
2014-11-06 15:17:08 +01:00
|
|
|
{
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << "Constructing output string\n";
|
2014-11-06 15:17:08 +01:00
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
std::vector<std::wstring> wsDrives, wsPerf;
|
|
|
|
std::wstring unit = BunitStr(printInfo.unit);
|
2014-11-06 15:17:08 +01:00
|
|
|
|
2015-01-23 16:34:16 +01:00
|
|
|
state state = OK;
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wstring output = L"DISK OK - free space:";
|
2014-11-06 15:17:08 +01:00
|
|
|
|
2015-01-23 16:34:16 +01:00
|
|
|
double tCap = 0, tFree = 0;
|
2015-03-23 13:07:02 +01:00
|
|
|
for (std::vector<drive>::iterator it = vDrives.begin(); it != vDrives.end(); it++) {
|
2015-01-23 16:34:16 +01:00
|
|
|
tCap += it->cap;
|
|
|
|
tFree += it->free;
|
|
|
|
wsDrives.push_back(it->name + L" " + removeZero(it->free) + L" " + unit + L" (" +
|
|
|
|
removeZero(std::round(it->free/it->cap * 100.0)) + L"%); ");
|
|
|
|
wsPerf.push_back(L" " + it->name + L"=" + removeZero(it->free) + unit + L";" +
|
|
|
|
printInfo.warn.pString(it->cap) + L";" + printInfo.crit.pString(it->cap) + L";0;"
|
|
|
|
+ removeZero(it->cap));
|
2015-06-22 12:57:37 +02:00
|
|
|
if (printInfo.crit.rend(it->free, it->cap))
|
|
|
|
state = CRITICAL;
|
|
|
|
if (state == OK && printInfo.warn.rend(it->free, it->cap))
|
|
|
|
state = WARNING;
|
2014-11-06 15:17:08 +01:00
|
|
|
}
|
2014-11-06 16:36:42 +01:00
|
|
|
|
2015-06-22 12:57:37 +02:00
|
|
|
if (state == WARNING)
|
2015-01-23 16:34:16 +01:00
|
|
|
output = L"DISK WARNING - free space:";
|
2014-11-06 15:17:08 +01:00
|
|
|
|
2015-06-22 12:57:37 +02:00
|
|
|
if (state == CRITICAL)
|
2015-01-23 16:34:16 +01:00
|
|
|
output = L"DISK CRITICAL - free space:";
|
2014-11-06 15:17:08 +01:00
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << output;
|
2015-01-23 16:34:16 +01:00
|
|
|
if (vDrives.size() > 1)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << "Total " << tFree << unit << " (" << removeZero(std::round(tFree/tCap * 100.0)) << "%); ";
|
2015-01-23 16:34:16 +01:00
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
for (std::vector<std::wstring>::const_iterator it = wsDrives.begin(); it != wsDrives.end(); it++)
|
|
|
|
std::wcout << *it;
|
|
|
|
std::wcout << "|";
|
2015-01-23 16:34:16 +01:00
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
for (std::vector<std::wstring>::const_iterator it = wsPerf.begin(); it != wsPerf.end(); it++)
|
|
|
|
std::wcout << *it;
|
2015-01-23 16:34:16 +01:00
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << '\n';
|
2014-11-06 15:17:08 +01:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2015-06-23 13:15:55 +02:00
|
|
|
static INT check_drives(std::vector<drive>& vDrives, std::vector<std::wstring>& vExclude_Drives)
|
2014-11-06 15:17:08 +01:00
|
|
|
{
|
|
|
|
DWORD dwResult, dwSize = 0, dwVolumePathNamesLen = MAX_PATH + 1;
|
2015-03-23 13:07:02 +01:00
|
|
|
WCHAR szLogicalDrives[1024], szVolumeName[MAX_PATH], *szVolumePathNames = NULL;
|
2015-02-17 15:54:23 +01:00
|
|
|
HANDLE hVolume = NULL;
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wstring wsLogicalDrives;
|
2014-11-06 15:17:08 +01:00
|
|
|
size_t volumeNameEnd = 0;
|
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
std::set<std::wstring> sDrives;
|
2014-11-06 15:17:08 +01:00
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << "Getting logic drive string (includes network drives)\n";
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2014-11-06 15:17:08 +01:00
|
|
|
dwResult = GetLogicalDriveStrings(MAX_PATH, szLogicalDrives);
|
2015-03-02 13:18:08 +01:00
|
|
|
if (dwResult > MAX_PATH)
|
2014-11-06 15:17:08 +01:00
|
|
|
goto die;
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-06-23 13:15:55 +02:00
|
|
|
std::wcout << "Splitting string intoo single drive names\n";
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2014-11-06 15:17:08 +01:00
|
|
|
LPTSTR szSingleDrive = szLogicalDrives;
|
|
|
|
while (*szSingleDrive) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wstring drname = szSingleDrive;
|
2014-11-06 15:17:08 +01:00
|
|
|
sDrives.insert(drname);
|
|
|
|
szSingleDrive += wcslen(szSingleDrive) + 1;
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << "Got: " << drname << '\n';
|
2014-11-06 15:17:08 +01:00
|
|
|
}
|
|
|
|
|
2015-06-23 13:15:55 +02:00
|
|
|
if (debug)
|
|
|
|
std::wcout << "Getting volume mountpoints (includes NTFS folders)\n"
|
|
|
|
<< "Getting first volume\n";
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2014-11-06 15:17:08 +01:00
|
|
|
hVolume = FindFirstVolume(szVolumeName, MAX_PATH);
|
|
|
|
if (hVolume == INVALID_HANDLE_VALUE)
|
|
|
|
goto die;
|
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << "Traversing through list of drives\n";
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2014-11-06 15:17:08 +01:00
|
|
|
while (GetLastError() != ERROR_NO_MORE_FILES) {
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << "Path name for " << szVolumeName << "= \"";
|
2014-11-06 15:17:08 +01:00
|
|
|
volumeNameEnd = wcslen(szVolumeName) - 1;
|
2015-03-23 13:07:02 +01:00
|
|
|
szVolumePathNames = reinterpret_cast<WCHAR*>(new WCHAR[dwVolumePathNamesLen]);
|
2014-11-06 15:17:08 +01:00
|
|
|
|
2015-06-23 13:15:55 +02:00
|
|
|
while (!GetVolumePathNamesForVolumeName(szVolumeName, szVolumePathNames, dwVolumePathNamesLen,
|
|
|
|
&dwVolumePathNamesLen)) {
|
2014-11-06 15:17:08 +01:00
|
|
|
if (GetLastError() != ERROR_MORE_DATA)
|
|
|
|
break;
|
2015-03-23 13:07:02 +01:00
|
|
|
delete[] reinterpret_cast<WCHAR*>(szVolumePathNames);
|
|
|
|
szVolumePathNames = reinterpret_cast<WCHAR*>(new WCHAR[dwVolumePathNamesLen]);
|
2014-11-06 15:17:08 +01:00
|
|
|
|
|
|
|
}
|
2015-01-13 09:41:53 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << szVolumePathNames << "\"\n";
|
2014-11-06 15:17:08 +01:00
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
sDrives.insert(std::wstring(szVolumePathNames));
|
2014-11-06 15:17:08 +01:00
|
|
|
FindNextVolume(hVolume, szVolumeName, MAX_PATH);
|
|
|
|
}
|
2015-01-13 09:51:49 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << "Creating vector from found volumes, ignoring cd drives etc.:\n";
|
|
|
|
for (std::set<std::wstring>::iterator it = sDrives.begin(); it != sDrives.end(); ++it) {
|
2014-11-06 15:17:08 +01:00
|
|
|
UINT type = GetDriveType(it->c_str());
|
|
|
|
if (type == DRIVE_FIXED || type == DRIVE_REMOTE) {
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << "\t" << *it << '\n';
|
2014-11-06 15:17:08 +01:00
|
|
|
vDrives.push_back(drive(*it));
|
|
|
|
}
|
|
|
|
}
|
2015-01-12 15:53:25 +01:00
|
|
|
|
|
|
|
FindVolumeClose(hVolume);
|
2015-02-17 15:54:23 +01:00
|
|
|
if (szVolumePathNames)
|
2015-03-23 13:07:02 +01:00
|
|
|
delete[] reinterpret_cast<WCHAR*>(szVolumePathNames);
|
2015-06-23 13:15:55 +02:00
|
|
|
|
|
|
|
if (!vExclude_Drives.empty()) {
|
|
|
|
if (debug)
|
|
|
|
std::wcout << "Removing excluded drives\n";
|
|
|
|
|
|
|
|
BOOST_FOREACH(const std::wstring wsDriveName, vExclude_Drives)
|
|
|
|
{
|
|
|
|
vDrives.erase(std::remove_if(vDrives.begin(), vDrives.end(),
|
|
|
|
boost::bind(checkName, _1, wsDriveName + L'\\')), vDrives.end());
|
|
|
|
}
|
|
|
|
}
|
2014-11-06 15:17:08 +01:00
|
|
|
return -1;
|
2015-06-23 13:15:55 +02:00
|
|
|
|
2014-11-06 15:17:08 +01:00
|
|
|
die:
|
|
|
|
if (hVolume)
|
|
|
|
FindVolumeClose(hVolume);
|
|
|
|
die();
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2015-06-23 13:15:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
static INT check_drives(std::vector<drive>& vDrives, printInfoStruct& printInfo)
|
2014-11-06 15:17:08 +01:00
|
|
|
{
|
2015-06-23 13:15:55 +02:00
|
|
|
if (!printInfo.exclude_drives.empty()) {
|
|
|
|
if (debug)
|
|
|
|
std::wcout << "Removing excluded drives from user input drives\n";
|
|
|
|
BOOST_FOREACH(std::wstring wsDrive, printInfo.exclude_drives)
|
|
|
|
{
|
|
|
|
printInfo.drives.erase(std::remove(printInfo.drives.begin(), printInfo.drives.end(), wsDrive),
|
|
|
|
printInfo.drives.end());
|
|
|
|
}
|
|
|
|
}
|
2014-11-06 15:17:08 +01:00
|
|
|
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << "Parsing user input drive names\n";
|
2015-01-12 15:53:25 +01:00
|
|
|
|
2015-03-23 13:07:02 +01:00
|
|
|
for (std::vector<std::wstring>::iterator it = printInfo.drives.begin();
|
2014-11-06 15:17:08 +01:00
|
|
|
it != printInfo.drives.end(); ++it) {
|
2015-06-23 13:15:55 +02:00
|
|
|
if (it->at(it->length() - 1) != *L"\\")
|
|
|
|
it->append(L"\\");
|
2014-12-19 10:21:38 +01:00
|
|
|
if (std::wstring::npos == it->find(L":\\")) {
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << "A \":\" is required after the drive name of " << *it << '\n';
|
2014-12-19 10:21:38 +01:00
|
|
|
return 3;
|
|
|
|
}
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << "Added " << *it << '\n';
|
2014-11-06 15:17:08 +01:00
|
|
|
vDrives.push_back(drive(*it));
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 13:15:55 +02:00
|
|
|
static BOOL getFreeAndCap(drive& drive, const Bunit& unit)
|
2014-11-06 15:17:08 +01:00
|
|
|
{
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << "Getting free disk space for drive " << drive.name << '\n';
|
2014-11-06 15:17:08 +01:00
|
|
|
ULARGE_INTEGER tempFree, tempTotal;
|
|
|
|
if (!GetDiskFreeSpaceEx(drive.name.c_str(), NULL, &tempTotal, &tempFree)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << "\tcap: " << tempFree.QuadPart << '\n';
|
2014-12-19 10:21:38 +01:00
|
|
|
drive.cap = round((tempTotal.QuadPart / pow(1024.0, unit)));
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << "\tAfter conversion: " << drive.cap << '\n'
|
2015-06-23 13:15:55 +02:00
|
|
|
<< "\tfree: " << tempFree.QuadPart << '\n';
|
2014-12-19 10:21:38 +01:00
|
|
|
drive.free = round((tempFree.QuadPart / pow(1024.0, unit)));
|
2015-01-12 15:53:25 +01:00
|
|
|
if (debug)
|
2015-03-23 13:07:02 +01:00
|
|
|
std::wcout << "\tAfter conversion: " << drive.free << '\n' << '\n';
|
2014-11-06 15:17:08 +01:00
|
|
|
|
|
|
|
return TRUE;
|
2015-02-10 09:32:14 +01:00
|
|
|
}
|
2015-06-23 13:15:55 +02:00
|
|
|
|
|
|
|
static bool checkName(const drive& d, const std::wstring& s)
|
|
|
|
{
|
|
|
|
return (s == d.name);
|
|
|
|
}
|
|
|
|
|