Skip to content
Snippets Groups Projects
Commit 2f4016cc authored by Lais Frigerio's avatar Lais Frigerio
Browse files

SCRUM#87 - Get distro info on windows

parent efc60514
No related branches found
No related tags found
No related merge requests found
Pipeline #
......@@ -5,3 +5,4 @@ test/docker/*/*.sh
log/*
styleguide/*
Documentation/*
.vs/*
......@@ -6,11 +6,17 @@ project( simmc-agent )
set ( VERSION_MAJOR 0 )
set ( VERSION_MINOR 0 )
# cpr requires c++11
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
if(UNIX)
# cpr requires c++11
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
else(WIN32)
add_definitions( -DBOOST_ALL_NO_LIB )
set(Boost_USE_STATIC_LIBS ON)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
endif()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc")
# src : main, collect functions + jsoncpp library
file ( GLOB SOURCES src/agent/*.cpp )
......@@ -35,7 +41,7 @@ include_directories ( ${CURL_INCLUDE_DIRS} )
# src : DtWinVer - Windows Version/Edition class
if ( WIN32 )
add_subdirectory ( lib/dtwinver )
# add_subdirectory ( lib/dtwinver )
endif ()
# headers
......@@ -55,7 +61,7 @@ target_link_libraries ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} ${CPR_LIBRARIES
add_dependencies ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} Boost)
if ( WIN32 )
target_link_libraries ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} dtwinver )
# target_link_libraries ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} dtwinver )
else ()
# libudev
target_link_libraries ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} udev )
......
#pragma once
#include <iostream>
#include <sys/utsname.h>
#include <fstream>
#include <cpr/cpr.h>
#include <json/json.h>
......@@ -11,6 +10,10 @@
#include <agent/parse_proxy_file.h>
#include <agent/rotate_log_file.h>
#ifdef __linux__
#include <sys/utsname.h>
#endif
const std::string url = "http://localhost:3001";
/**
......
#pragma once
#include <libudev.h>
#include <iostream>
#include <fstream>
#include <list>
#include <string>
#ifdef __linux__
#include <libudev.h>
#endif
/**
* @file get_disks_info.h
* @brief Get 2 disks info (Model and size for each one)
......
#pragma once
#include <sys/utsname.h>
#include <iostream>
#include <string>
#ifdef __linux__
#include <sys/utsname.h>
#else
#include <windows.h>
#include <windows.h>
#include <VersionHelpers.h>
#endif
#include <agent/open_file.h>
/**
* @file get_distro.h
* @brief Get info from Ubuntu, debian, Linux Comunicações, Linux Educacional, fedora and OpenSUSE distro; windows use dtwinver
......
......@@ -61,7 +61,12 @@ bool Curl::downloadAgent(std::string url, std::fstream* logFile,
agent->setRunName("agent.run");
boost::filesystem::path test_file(test_root / agent->getRunName());
FILE* fp = fopen(test_file.c_str(), "wb");
#ifdef WIN32
std::string var = test_file.string();
FILE* fp = fopen(var.c_str(), "wb");
#else
FILE* fp = fopen(test_file.c_str(), "wb");
#endif
if (!fp) {
agent->setMessage(__DATE__ + std::string(" - ERROR: Failed to ") +
......
......@@ -20,6 +20,7 @@
*/
#include <agent/get_disks_info.h>
#ifdef __linux__
/** We use here udev lib to get info about disks */
void get_scsi_disks(std::list<disk_t>& disks) { // NOLINT(runtime/references)
......@@ -109,7 +110,7 @@ void get_disks_info(std::list<disk_t>& disks) { // NOLINT(runtime/references)
throw std::string("No disk info found");
}
}
#endif
/**
* @file get_disks_info.cpp
* @brief Get 2 disks info (Model and size for each one)
......
......@@ -32,119 +32,152 @@
* - OpenSUSE info at /etc/redhat-release file;
*/
std::string get_distro() {
/* currently recorded distros are
/* currently recorded distros on Linux are
* Ubuntu
* Debian
* Linux Comunicações
* Linux Educacional
all of which should have the lsb-release file, but I added a couple more
common distros just in case */
std::ifstream release_file;
std::string line;
// lsb compliant
if (open_file("/etc/lsb-release", release_file)) {
const std::string key = "DISTRIB_DESCRIPTION=";
std::size_t found;
while (getline(release_file, line)) {
found = line.find(key);
if (found != std::string::npos) {
return (line.substr(found+key.length()+1,
// remove key and quotes at the beginning
line.length()-(found+key.length()+1)-1));
// remove quotes at the end
common distros just in case
Windows com o header <VersionHelpers.h>, we could get:
* XP
* Vista
* WIN7
* WIN8
* WIN10
*/
#ifdef __linux__
std::ifstream release_file;
std::string line;
// lsb compliant
if (open_file("/etc/lsb-release", release_file)) {
const std::string key = "DISTRIB_DESCRIPTION=";
std::size_t found;
while (getline(release_file, line)) {
found = line.find(key);
if (found != std::string::npos) {
return (line.substr(found+key.length()+1,
// remove key and quotes at the beginning
line.length()-(found+key.length()+1)-1));
// remove quotes at the end
}
}
}
throw std::string("Release file empty or incomplete.");
throw std::string("Release file empty or incomplete.");
// openSuSE
} else if (open_file("/etc/os-release", release_file)) {
const std::string name_key = "NAME=";
const std::string version_key = "VERSION=";
std::string name;
std::string version;
std::size_t found_name;
std::size_t found_version;
while (getline(release_file, line)) {
found_name = line.find(name_key);
found_version = line.find(version_key);
if (found_name < line.length()) {
name = line.substr(found_name+name_key.length()+1,
// remove key and quotes at the beginning
line.length()-(found_name+name_key.length()+1)-1);
// remove quotes at the end
}
if (found_version < line.length()) {
version = line.substr(found_version+version_key.length()+1,
// remove key and quotes at the beginning
line.length()-
(found_version+version_key.length()+1)-1);
// remove quotes at the end
}
if (name.length() && version.length()) {
return name + version;
}
}
// openSuSE
} else if (open_file("/etc/os-release", release_file)) {
const std::string name_key = "NAME=";
const std::string version_key = "VERSION=";
if (name.length())
return name;
std::string name;
std::string version;
throw std::string("Release file empty or incomplete.");
std::size_t found_name;
std::size_t found_version;
} else if (open_file("/etc/novell-release", release_file)) {
getline(release_file, line);
while (getline(release_file, line)) {
found_name = line.find(name_key);
found_version = line.find(version_key);
if (found_name < line.length()) {
name = line.substr(found_name+name_key.length()+1,
// remove key and quotes at the beginning
line.length()-(found_name+name_key.length()+1)-1);
// remove quotes at the end
}
if (line.length())
return line;
if (found_version < line.length()) {
version = line.substr(found_version+version_key.length()+1,
// remove key and quotes at the beginning
line.length()-
(found_version+version_key.length()+1)-1);
// remove quotes at the end
}
throw std::string("Release file empty.");
if (name.length() && version.length()) {
return name + version;
}
}
} else if (open_file("/etc/sles-release", release_file)) {
getline(release_file, line);
if (line.length())
return line;
throw std::string("Release file empty.");
if (name.length())
return name;
// fedora
} else if (open_file("/etc/redhat-release", release_file)) {
getline(release_file, line);
throw std::string("Release file empty or incomplete.");
if (line.length())
return line;
} else if (open_file("/etc/novell-release", release_file)) {
getline(release_file, line);
throw std::string("Release file empty.");
if (line.length())
return line;
// debian -- leave this at the end, otherwise debian-based distros
// will find the debian_version file instead of the actual
// distro name
} else if (open_file("/etc/debian-release", release_file)) {
getline(release_file, line);
throw std::string("Release file empty.");
if (line.length())
return line;
} else if (open_file("/etc/sles-release", release_file)) {
getline(release_file, line);
throw std::string("Release file empty.");
if (line.length())
return line;
} else if (open_file("/etc/debian-version", release_file)) {
getline(release_file, line);
throw std::string("Release file empty.");
if (line.length())
return line;
throw std::string("Release file empty.");
}
// fedora
} else if (open_file("/etc/redhat-release", release_file)) {
getline(release_file, line);
throw std::string("No release file found.");
if (line.length())
return line;
#else
throw std::string("Release file empty.");
if (IsWindows10OrGreater())
return "Windows 10";
// debian -- leave this at the end, otherwise debian-based distros
// will find the debian_version file instead of the actual
// distro name
} else if (open_file("/etc/debian-release", release_file)) {
getline(release_file, line);
if (IsWindows8Point1OrGreater())
return "Windows 8.1";
if (line.length())
return line;
if (IsWindows8OrGreater()
return "Windows 8";
throw std::string("Release file empty.");
if (IsWindows7OrGreater())
return "Windows 7";
} else if (open_file("/etc/debian-version", release_file)) {
getline(release_file, line);
if (IsWindowsVistaOrGreater()
return "Windows Vista";
if (line.length())
return line;
if (IsWindowsXPOrGreater())
return "Windows XP";
throw std::string("Release file empty.");
}
throw std::string("unknown distro");
throw std::string("No release file found.");
#endif
}
/**
......
......@@ -59,7 +59,7 @@ Json::Value get_inventory() {
try {
#ifdef WIN32
// use dtwinver
inv["data_inventory"]["os_distro"] = "unknown";
inv["data_inventory"]["os_distro"] = get_distro();
#else
inv["data_inventory"]["os_distro"] = get_distro();
#endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment