/src/libs/zbxsysinfo/osf/uptime.c

https://github.com/Shmuma/z · C · 120 lines · 73 code · 24 blank · 23 comment · 10 complexity · d7a5d74761cabbd686ed538a231ec012 MD5 · raw file

  1. /*
  2. ** ZABBIX
  3. ** Copyright (C) 2000-2005 SIA Zabbix
  4. **
  5. ** This program is free software; you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation; either version 2 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program; if not, write to the Free Software
  17. ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. **/
  19. #include "common.h"
  20. #include "sysinfo.h"
  21. int SYSTEM_UPTIME(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result)
  22. {
  23. #ifdef HAVE_SYSINFO_UPTIME
  24. struct sysinfo info;
  25. assert(result);
  26. init_result(result);
  27. if( 0 == sysinfo(&info))
  28. {
  29. SET_UI64_RESULT(result, info.uptime);
  30. return SYSINFO_RET_OK;
  31. }
  32. else
  33. {
  34. return SYSINFO_RET_FAIL;
  35. }
  36. #else
  37. #ifdef HAVE_FUNCTION_SYSCTL_KERN_BOOTTIME
  38. int mib[2],len;
  39. struct timeval uptime;
  40. int now;
  41. assert(result);
  42. init_result(result);
  43. mib[0]=CTL_KERN;
  44. mib[1]=KERN_BOOTTIME;
  45. len=sizeof(uptime);
  46. if(sysctl(mib,2,&uptime,(size_t *)&len,NULL,0) != 0)
  47. {
  48. return SYSINFO_RET_FAIL;
  49. /* printf("Errno [%m]\n");*/
  50. }
  51. now=time(NULL);
  52. SET_UI64_RESULT(result, now-uptime.tv_sec);
  53. return SYSINFO_RET_OK;
  54. #else
  55. /* Solaris */
  56. #ifdef HAVE_KSTAT_H
  57. kstat_ctl_t *kc;
  58. kstat_t *kp;
  59. kstat_named_t *kn;
  60. long hz;
  61. long secs;
  62. assert(result);
  63. init_result(result);
  64. hz = sysconf(_SC_CLK_TCK);
  65. /* open kstat */
  66. kc = kstat_open();
  67. if (0 == kc)
  68. {
  69. return SYSINFO_RET_FAIL;
  70. }
  71. /* read uptime counter */
  72. kp = kstat_lookup(kc, "unix", 0, "system_misc");
  73. if (0 == kp)
  74. {
  75. kstat_close(kc);
  76. return SYSINFO_RET_FAIL;
  77. }
  78. if(-1 == kstat_read(kc, kp, 0))
  79. {
  80. kstat_close(kc);
  81. return SYSINFO_RET_FAIL;
  82. }
  83. kn = (kstat_named_t*)kstat_data_lookup(kp, "clk_intr");
  84. secs = kn->value.ul / hz;
  85. /* close kstat */
  86. kstat_close(kc);
  87. SET_UI64_RESULT(result, secs);
  88. return SYSINFO_RET_OK;
  89. #else
  90. assert(result);
  91. init_result(result);
  92. return SYSINFO_RET_FAIL;
  93. #endif
  94. #endif
  95. #endif
  96. }