mirror of
https://github.com/badaix/snapcast.git
synced 2025-07-20 01:47:36 +02:00
added uptime
This commit is contained in:
parent
de8c4e4ea2
commit
f546738d75
1 changed files with 45 additions and 41 deletions
|
@ -34,19 +34,21 @@
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
#include <sys/sysinfo.h>
|
||||||
|
|
||||||
|
|
||||||
// trim from start
|
// trim from start
|
||||||
static inline std::string <rim(std::string &s)
|
static inline std::string <rim(std::string &s)
|
||||||
{
|
{
|
||||||
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
|
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
// trim from end
|
// trim from end
|
||||||
static inline std::string &rtrim(std::string &s)
|
static inline std::string &rtrim(std::string &s)
|
||||||
{
|
{
|
||||||
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
|
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
// trim from both ends
|
// trim from both ends
|
||||||
|
@ -58,21 +60,21 @@ static inline std::string &trim(std::string &s)
|
||||||
// trim from start
|
// trim from start
|
||||||
static inline std::string ltrim_copy(const std::string &s)
|
static inline std::string ltrim_copy(const std::string &s)
|
||||||
{
|
{
|
||||||
std::string str(s);
|
std::string str(s);
|
||||||
return ltrim(str);
|
return ltrim(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
// trim from end
|
// trim from end
|
||||||
static inline std::string rtrim_copy(const std::string &s)
|
static inline std::string rtrim_copy(const std::string &s)
|
||||||
{
|
{
|
||||||
std::string str(s);
|
std::string str(s);
|
||||||
return rtrim(str);
|
return rtrim(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
// trim from both ends
|
// trim from both ends
|
||||||
static inline std::string trim_copy(const std::string &s)
|
static inline std::string trim_copy(const std::string &s)
|
||||||
{
|
{
|
||||||
std::string str(s);
|
std::string str(s);
|
||||||
return trim(str);
|
return trim(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,52 +88,54 @@ static std::string getHostName()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static long uptime()
|
||||||
|
{
|
||||||
|
struct sysinfo info;
|
||||||
|
sysinfo(&info);
|
||||||
|
return info.uptime;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static std::string getMacAddress(int sock)
|
static std::string getMacAddress(int sock)
|
||||||
{
|
{
|
||||||
struct ifreq ifr;
|
struct ifreq ifr;
|
||||||
struct ifconf ifc;
|
struct ifconf ifc;
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
int success = 0;
|
int success = 0;
|
||||||
|
|
||||||
if (sock < 0)
|
if (sock < 0)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
ifc.ifc_len = sizeof(buf);
|
ifc.ifc_len = sizeof(buf);
|
||||||
ifc.ifc_buf = buf;
|
ifc.ifc_buf = buf;
|
||||||
if (ioctl(sock, SIOCGIFCONF, &ifc) == -1)
|
if (ioctl(sock, SIOCGIFCONF, &ifc) == -1)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
struct ifreq* it = ifc.ifc_req;
|
struct ifreq* it = ifc.ifc_req;
|
||||||
const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq));
|
const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq));
|
||||||
|
|
||||||
for (; it != end; ++it) {
|
for (; it != end; ++it) {
|
||||||
strcpy(ifr.ifr_name, it->ifr_name);
|
strcpy(ifr.ifr_name, it->ifr_name);
|
||||||
if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {
|
if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {
|
||||||
if (! (ifr.ifr_flags & IFF_LOOPBACK)) { // don't count loopback
|
if (! (ifr.ifr_flags & IFF_LOOPBACK)) { // don't count loopback
|
||||||
if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {
|
if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {
|
||||||
success = 1;
|
success = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else { /* handle error */ }
|
else { /* handle error */ }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!success)
|
if (!success)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
char mac[19];
|
char mac[19];
|
||||||
sprintf(mac, "%02x:%02x:%02x:%02x:%02x:%02x",
|
sprintf(mac, "%02x:%02x:%02x:%02x:%02x:%02x",
|
||||||
(unsigned char)ifr.ifr_hwaddr.sa_data[0], (unsigned char)ifr.ifr_hwaddr.sa_data[1], (unsigned char)ifr.ifr_hwaddr.sa_data[2],
|
(unsigned char)ifr.ifr_hwaddr.sa_data[0], (unsigned char)ifr.ifr_hwaddr.sa_data[1], (unsigned char)ifr.ifr_hwaddr.sa_data[2],
|
||||||
(unsigned char)ifr.ifr_hwaddr.sa_data[3], (unsigned char)ifr.ifr_hwaddr.sa_data[4], (unsigned char)ifr.ifr_hwaddr.sa_data[5]);
|
(unsigned char)ifr.ifr_hwaddr.sa_data[3], (unsigned char)ifr.ifr_hwaddr.sa_data[4], (unsigned char)ifr.ifr_hwaddr.sa_data[5]);
|
||||||
return mac;
|
return mac;
|
||||||
}
|
}
|
||||||
/* std::ifstream t("/sys/class/net/eth0/address");
|
|
||||||
std::string str((std::istreambuf_iterator<char>(t)),
|
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
return trim(str);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue