mirror of
https://gitlab.com/Syping/mayu
synced 2024-11-25 05:20:23 +01:00
add clean for remove -1 results
This commit is contained in:
parent
16d5d83eec
commit
d5663e583b
3 changed files with 53 additions and 11 deletions
18
main.cpp
18
main.cpp
|
@ -31,6 +31,7 @@ int main(int argc, char *argv[])
|
||||||
QStringList arguments = a.arguments();
|
QStringList arguments = a.arguments();
|
||||||
arguments.removeAt(0);
|
arguments.removeAt(0);
|
||||||
|
|
||||||
|
bool a_clean = false;
|
||||||
mayuMode a_mode = mayuMode::Ping;
|
mayuMode a_mode = mayuMode::Ping;
|
||||||
for (int i = arguments.length(); i > 0; i--) {
|
for (int i = arguments.length(); i > 0; i--) {
|
||||||
const QString &argument = arguments.at(i-1);
|
const QString &argument = arguments.at(i-1);
|
||||||
|
@ -42,16 +43,31 @@ int main(int argc, char *argv[])
|
||||||
a_mode = mayuMode::Resolve;
|
a_mode = mayuMode::Resolve;
|
||||||
arguments.removeAt(i-1);
|
arguments.removeAt(i-1);
|
||||||
}
|
}
|
||||||
|
else if (argument == "-c" || argument == "--clean") {
|
||||||
|
a_clean = true;
|
||||||
|
arguments.removeAt(i-1);
|
||||||
|
}
|
||||||
|
else if (argument == "-pc" || argument == "-cp") {
|
||||||
|
a_mode = mayuMode::Ping;
|
||||||
|
a_clean = true;
|
||||||
|
arguments.removeAt(i-1);
|
||||||
|
}
|
||||||
|
else if (argument == "-rc" || argument == "-cr") {
|
||||||
|
a_mode = mayuMode::Resolve;
|
||||||
|
a_clean = true;
|
||||||
|
arguments.removeAt(i-1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arguments.length() >= 2) {
|
if (arguments.length() >= 2) {
|
||||||
mayu a_mayu(arguments.at(0), arguments.at(1));
|
mayu a_mayu(arguments.at(0), arguments.at(1));
|
||||||
a_mayu.setMayuMode(a_mode);
|
a_mayu.setMayuMode(a_mode);
|
||||||
|
a_mayu.setCleanUp(a_clean);
|
||||||
a_mayu.work();
|
a_mayu.work();
|
||||||
return a_mayu.getResult();
|
return a_mayu.getResult();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
QTextStream(stdout) << "Usage: " << a.arguments().at(0) << " [-p ping]" << " [-r resolve]" << " input.txt" << " output.json" << endl;
|
QTextStream(stdout) << "Usage: " << a.arguments().at(0) << " [-p ping]" << " [-r resolve]" << " [-c clean]" << " input.txt" << " output.json" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
41
mayu.cpp
41
mayu.cpp
|
@ -37,6 +37,7 @@ mayu::mayu(const QString &hostsFile, const QString &jsonFile, QObject *parent) :
|
||||||
p_return = -1;
|
p_return = -1;
|
||||||
p_timeout = 2.5;
|
p_timeout = 2.5;
|
||||||
p_tries = 4;
|
p_tries = 4;
|
||||||
|
p_clean = false;
|
||||||
p_mayuMode = mayuMode::Ping;
|
p_mayuMode = mayuMode::Ping;
|
||||||
if (!hostsFile.isEmpty())
|
if (!hostsFile.isEmpty())
|
||||||
setHostsFile(hostsFile);
|
setHostsFile(hostsFile);
|
||||||
|
@ -76,6 +77,11 @@ void mayu::setPingTries(int tries)
|
||||||
p_tries = tries;
|
p_tries = tries;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void mayu::setCleanUp(bool clean)
|
||||||
|
{
|
||||||
|
p_clean = clean;
|
||||||
|
}
|
||||||
|
|
||||||
mayuMode mayu::getMayuMode()
|
mayuMode mayu::getMayuMode()
|
||||||
{
|
{
|
||||||
return p_mayuMode;
|
return p_mayuMode;
|
||||||
|
@ -106,6 +112,11 @@ int mayu::getPingTries()
|
||||||
return p_tries;
|
return p_tries;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool mayu::getCleanUp()
|
||||||
|
{
|
||||||
|
return p_clean;
|
||||||
|
}
|
||||||
|
|
||||||
int mayu::getResult()
|
int mayu::getResult()
|
||||||
{
|
{
|
||||||
return p_return;
|
return p_return;
|
||||||
|
@ -184,7 +195,14 @@ double mayu::ping(const QString &host, int tries, double timeout)
|
||||||
char hostname[100];
|
char hostname[100];
|
||||||
len = 100;
|
len = 100;
|
||||||
ping_iterator_get_info(pingIter, PING_INFO_HOSTNAME, hostname, &len);
|
ping_iterator_get_info(pingIter, PING_INFO_HOSTNAME, hostname, &len);
|
||||||
QTextStream(stdout) << "Host: " << hostname << " Ping: " << latency << "ms" << " Status: " << (pingSuccess ? "true" : "false") << endl;
|
QString latencyString;
|
||||||
|
if (latency != -1) {
|
||||||
|
latencyString = QString::number(latency) + "ms";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
latencyString = QString::number(latency);
|
||||||
|
}
|
||||||
|
QTextStream(stdout) << "Host: " << hostname << " Ping: " << latencyString << " Status: " << (pingSuccess ? "true" : "false") << endl;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
if (pingSuccess) {
|
if (pingSuccess) {
|
||||||
|
@ -199,7 +217,7 @@ double mayu::ping(const QString &host, int tries, double timeout)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
const QList<mayuResult> mayu::resolve(const QString &host)
|
const QList<mayuResult> mayu::resolve(const QString &host, bool emptyWhenError)
|
||||||
{
|
{
|
||||||
QList<mayuResult> resultList;
|
QList<mayuResult> resultList;
|
||||||
QList<QHostAddress> hostAddresses = QHostInfo::fromName(host).addresses();
|
QList<QHostAddress> hostAddresses = QHostInfo::fromName(host).addresses();
|
||||||
|
@ -218,10 +236,12 @@ const QList<mayuResult> mayu::resolve(const QString &host)
|
||||||
#ifdef E_DEBUG
|
#ifdef E_DEBUG
|
||||||
qDebug() << "Hostname" << host << "not found";
|
qDebug() << "Hostname" << host << "not found";
|
||||||
#endif
|
#endif
|
||||||
mayuResult m_result;
|
if (!emptyWhenError) {
|
||||||
m_result.host = host;
|
mayuResult m_result;
|
||||||
m_result.result = "-1";
|
m_result.host = host;
|
||||||
resultList += m_result;
|
m_result.result = "-1";
|
||||||
|
resultList += m_result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return resultList;
|
return resultList;
|
||||||
}
|
}
|
||||||
|
@ -326,7 +346,9 @@ void mayu::p_workPing()
|
||||||
const QStringList hostsList = getHosts();
|
const QStringList hostsList = getHosts();
|
||||||
for (const QString &host : hostsList) {
|
for (const QString &host : hostsList) {
|
||||||
double result = ping(host, p_tries, p_timeout);
|
double result = ping(host, p_tries, p_timeout);
|
||||||
jsonObject[host] = result;
|
if (!(result == -1 && p_clean)) {
|
||||||
|
jsonObject[host] = result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
p_saveWork(jsonObject);
|
p_saveWork(jsonObject);
|
||||||
}
|
}
|
||||||
|
@ -339,12 +361,13 @@ void mayu::p_workResolve()
|
||||||
QJsonObject jsonObject;
|
QJsonObject jsonObject;
|
||||||
const QStringList hostsList = getHosts();
|
const QStringList hostsList = getHosts();
|
||||||
for (const QString &host : hostsList) {
|
for (const QString &host : hostsList) {
|
||||||
const QList<mayuResult> resultList = resolve(host);
|
const QList<mayuResult> resultList = resolve(host, p_clean);
|
||||||
QJsonArray arrayList;
|
QJsonArray arrayList;
|
||||||
for (const mayuResult &result : resultList) {
|
for (const mayuResult &result : resultList) {
|
||||||
arrayList += result.result;
|
arrayList += result.result;
|
||||||
}
|
}
|
||||||
jsonObject[host] = arrayList;
|
if (!arrayList.isEmpty())
|
||||||
|
jsonObject[host] = arrayList;
|
||||||
}
|
}
|
||||||
p_saveWork(jsonObject);
|
p_saveWork(jsonObject);
|
||||||
}
|
}
|
||||||
|
|
5
mayu.h
5
mayu.h
|
@ -42,17 +42,19 @@ public:
|
||||||
void setJsonFile(const QString &fileName);
|
void setJsonFile(const QString &fileName);
|
||||||
void setPingTimeout(double timeout);
|
void setPingTimeout(double timeout);
|
||||||
void setPingTries(int tries);
|
void setPingTries(int tries);
|
||||||
|
void setCleanUp(bool clean);
|
||||||
mayuMode getMayuMode();
|
mayuMode getMayuMode();
|
||||||
const QString getHostsFile();
|
const QString getHostsFile();
|
||||||
const QStringList getHosts();
|
const QStringList getHosts();
|
||||||
const QString getJsonFile();
|
const QString getJsonFile();
|
||||||
double getPingTimeout();
|
double getPingTimeout();
|
||||||
int getPingTries();
|
int getPingTries();
|
||||||
|
bool getCleanUp();
|
||||||
int getResult();
|
int getResult();
|
||||||
#ifdef MAYU_UNIX
|
#ifdef MAYU_UNIX
|
||||||
static double ping(const QString &host, int tries, double timeout = 2.5);
|
static double ping(const QString &host, int tries, double timeout = 2.5);
|
||||||
#endif
|
#endif
|
||||||
static const QList<mayuResult> resolve(const QString &host);
|
static const QList<mayuResult> resolve(const QString &host, bool emptyWhenError = false);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void parse_hosts();
|
void parse_hosts();
|
||||||
|
@ -76,6 +78,7 @@ private:
|
||||||
double p_timeout;
|
double p_timeout;
|
||||||
int p_return;
|
int p_return;
|
||||||
int p_tries;
|
int p_tries;
|
||||||
|
bool p_clean;
|
||||||
#ifdef PRIVILEGE_DROP_REQUIRED
|
#ifdef PRIVILEGE_DROP_REQUIRED
|
||||||
uid_t p_uid;
|
uid_t p_uid;
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue