C++Guns – RoboBlog blogging the bot

28.06.2023

Ganglia SSD Metrics

Filed under: Allgemein — Tags: — Thomas @ 14:06

GMETRIC="/usr/bin/gmetric -c /etc/ganglia/gmond.conf"
DEV=/dev/sda

temperature=$(sudo smartctl -A $DEV | grep Temperature_Celsius |  awk '{print $10}')
Reallocated_Sector_Ct=$(sudo smartctl -A $DEV | grep Reallocated_Sector_Ct |  awk '{print $10}')
Current_Pending_Sector=$(sudo smartctl -A $DEV | grep Current_Pending_Sector |  awk '{print $10}')
Offline_Uncorrectable=$(sudo smartctl -A $DEV | grep Offline_Uncorrectable |  awk '{print $10}')
UDMA_CRC_Error_Count=$(sudo smartctl -A $DEV | grep UDMA_CRC_Error_Count |  awk '{print $10}')
Program_Fail_Count_Chip=$(sudo smartctl -A $DEV | grep Program_Fail_Count_Chip |  awk '{print $10}')
Erase_Fail_Count_Chip=$(sudo smartctl -A $DEV | grep Erase_Fail_Count_Chip |  awk '{print $10}')
Wear_Leveling_Count=$(sudo smartctl -A $DEV | grep Wear_Leveling_Count  |  awk '{print $10}')
Used_Rsvd_Blk_Cnt_Chip=$(sudo smartctl -A $DEV | grep Used_Rsvd_Blk_Cnt_Chip  |  awk '{print $10}')
Program_Fail_Cnt_Total=$(sudo smartctl -A $DEV | grep Program_Fail_Cnt_Total  |  awk '{print $10}')
Erase_Fail_Count_Total=$(sudo smartctl -A $DEV | grep Erase_Fail_Count_Total  |  awk '{print $10}')

echo "temperature" $temperature
echo "Reallocated_Sector_Ct" $Reallocated_Sector_Ct
echo "Current_Pending_Sector" $Current_Pending_Sector
echo "Offline_Uncorrectable" $Offline_Uncorrectable
echo "UDMA_CRC_Error_Count" $UDMA_CRC_Error_Count
echo "Program_Fail_Count_Chip" $Program_Fail_Count_Chip
echo "Erase_Fail_Count_Chip" $Erase_Fail_Count_Chip
echo "Wear_Leveling_Count " $Wear_Leveling_Count 
echo "Used_Rsvd_Blk_Cnt_Chip" $Used_Rsvd_Blk_Cnt_Chip
echo "Program_Fail_Cnt_Total" $Program_Fail_Cnt_Total
echo "Erase_Fail_Count_Total" $Erase_Fail_Count_Total


$GMETRIC  -t float -n "Temperature" -u "Celcius" -g "SSD" -v $temperature
$GMETRIC  -t float -n "Reallocated_Sector_Ct" -g "SSD" -v $Reallocated_Sector_Ct
$GMETRIC  -t float -n "Current_Pending_Sector" -g "SSD" -v $Current_Pending_Sector
$GMETRIC  -t float -n "Offline_Uncorrectable" -g "SSD" -v $Offline_Uncorrectable
$GMETRIC  -t float -n "UDMA_CRC_Error_Count" -g "SSD" -v $UDMA_CRC_Error_Count
$GMETRIC  -t float -n "Program_Fail_Count_Chip" -g "SSD" -v $Program_Fail_Count_Chip
$GMETRIC  -t float -n "Erase_Fail_Count_Chip" -g "SSD" -v $Erase_Fail_Count_Chip
$GMETRIC  -t float -n "Wear_Leveling_Count" -g "SSD" -v $Wear_Leveling_Count
$GMETRIC  -t float -n "Used_Rsvd_Blk_Cnt_Chip" -g "SSD" -v $Used_Rsvd_Blk_Cnt_Chip
$GMETRIC  -t float -n "Program_Fail_Cnt_Total" -g "SSD" -v $Program_Fail_Cnt_Total
$GMETRIC  -t float -n "Erase_Fail_Count_Total" -g "SSD" -v $Erase_Fail_Count_Total

19.06.2023

C++ Guns: How to convert from UTC to local time in C++?

Filed under: Allgemein — Tags: — Thomas @ 11:06

Convert from a broken down date time structure from UTC to localtime in C++.

See Stackoverflow: How to convert from UTC to local time in C?
I converted the code from C to C++ and make it shorter.

// replace non std function strptime with std::get_time

#include <iostream>
#include <sstream>
#include <iomanip>
#include <ctime>

/*
  Convert from a broken down date time structure from UTC to localtime.
  See https://stackoverflow.com/questions/9076494/how-to-convert-from-utc-to-local-time-in-c
*/
std::tm UTC2localtime(std::tm tp) {
  // make sure Daylight saving time flag is not accidentally switched on in UTC time
  tp.tm_isdst = 0;

  // get seconds since EPOCH for this time
  const time_t utc = std::mktime(&tp);
  std::cout << "UTC date and time in seconds since EPOCH: " << utc << "\n";

  // convert UTC date and time (Jan. 1, 1970) to local date and time
  std::tm e0{};
  e0.tm_mday = 1;
  e0.tm_year = 70;

  // get time_t EPOCH value for e0. This handles daylight saving stuff.
  // The value is e.g. -3600 for 1h difference between the timezones
  const time_t diff = std::mktime(&e0);

  // calculate local time in seconds since EPOCH
  const time_t local = utc - diff;
  std::cout << "local date and time in seconds since EPOCH: " << local << "\n";

  // convert seconds since EPOCH for local time into local_tm time structure
  std::tm local_tm;
  if(localtime_r(&local, &local_tm) == nullptr) {
    throw std::system_error(errno, std::generic_category(), "UTC2localtime(): in conversion vom UTC to localtime");
  }
  return local_tm;
}

int main() {
  // hard coded date and time in UTC
  std::string datetime = "2013 11 30 23 30 26";
  std::cout << "UTC date and time to be converted in local time: " << datetime << "\n";

  // put values of datetime into time structure
  std::tm UTC_tm{};
  std::istringstream ss(datetime);
  ss >> std::get_time(&UTC_tm, "%Y %m %d %H %M %S");

  if(ss.fail()) {
    throw std::runtime_error("Can not parse datetime from datetime '" + datetime + "' to format %Y %m %d %H %M %S");
  }

  const std::tm local_tm = UTC2localtime(UTC_tm);
  std::cout << "local date and time: " << std::put_time(&local_tm, "%Y-%m-%d %H:%M:%S %Z") << "\n";
}

Powered by WordPress