QNAP-TS419p system daemon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
5.5 KiB

  1. /***************************************************************************
  2. * Copyright (C) 12/2012 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  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 *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <sys/utsname.h>
  25. #include <sys/sysinfo.h>
  26. #include <time.h>
  27. #include "lcd.h"
  28. #include "lcdpage.h"
  29. /*
  30. * system:
  31. * - os version done
  32. * - kernel version done
  33. * - hostname done
  34. * - date/time done
  35. * - uptime done
  36. * - avgload done
  37. * - cpu usage
  38. * - memory/swap usage
  39. * - temperature done (pic.c)
  40. * - fan error done (pic.c)
  41. *
  42. * disk drives:
  43. * - temperature -> disk
  44. * - state (idle / running), time to idle -> disk
  45. * - current i/o (aggregated?)
  46. *
  47. * partitions:
  48. * - used / free / total
  49. *
  50. * ethernet interface:
  51. * - link (10/100/1000)
  52. * - current i/o
  53. *
  54. * ip interface:
  55. * - ip address/netmask
  56. * - default gateway
  57. * - dns
  58. *
  59. */
  60. static int lcdpage_system(struct lcddev *dev, int event, void *privdata)
  61. {
  62. static int subpage;
  63. int retval = 0;
  64. char line1[64];
  65. char line2[64];
  66. memset(line1, 0x00, sizeof(line1));
  67. memset(line2, 0x00, sizeof(line2));
  68. switch (event) {
  69. case LCDPAGE_EVENT_BUTTON1:
  70. lcd_set_backlight(dev, 1);
  71. subpage++;
  72. break;
  73. case LCDPAGE_EVENT_ENTER:
  74. lcd_set_backlight(dev, 1);
  75. subpage = 0;
  76. break;
  77. case LCDPAGE_EVENT_BACKLIGHT:
  78. case LCDPAGE_EVENT_EXIT:
  79. return 0;
  80. default:
  81. break;
  82. }
  83. if (subpage == 0) {
  84. FILE *fp = fopen("/etc/issue.net", "r");
  85. if (fp != NULL) {
  86. fread(line1, 1, sizeof(line1), fp);
  87. fclose(fp);
  88. char *trim = memchr(line1, 0x0A, sizeof(line1));
  89. if (trim != NULL)
  90. *trim = '\0';
  91. trim = memchr(line1, 0x0D, sizeof(line1));
  92. if (trim != NULL)
  93. *trim = '\0';
  94. } else {
  95. strcpy(line1, "<unknown>");
  96. }
  97. struct utsname utsbuf;
  98. uname(&utsbuf);
  99. snprintf(line2, sizeof(line2), "%s %s", utsbuf.sysname, utsbuf.release);
  100. lcd_setlines(dev, line1, line2);
  101. } else if (subpage == 1) {
  102. /* copy hostname */
  103. if (gethostname(line2, sizeof(line2)) < 0) {
  104. strcpy(line2, "<unknown>");
  105. }
  106. int len = strlen(line2);
  107. line2[len++] = '.';
  108. /* append domainname */
  109. if (getdomainname(line2 + len, sizeof(line2) - len) < 0) {
  110. strcpy(line2 + len, "unknown");
  111. }
  112. lcd_setlines(dev, "Hostname:", line2);
  113. } else if (subpage == 2) {
  114. struct tm tmp;
  115. time_t now;
  116. time(&now);
  117. localtime_r(&now, &tmp);
  118. strftime(line1, sizeof(line1), "Time: %H:%M:%S", &tmp);
  119. strftime(line2, sizeof(line2), "Date: %Y/%m/%d", &tmp);
  120. lcd_setlines(dev, line1, line2);
  121. retval = 1000; /* update every second */
  122. } else if (subpage == 3) {
  123. struct sysinfo info;
  124. if (sysinfo(&info) < 0) {
  125. return 0;
  126. }
  127. int tmp, days, hours, minutes;
  128. days = info.uptime / 86400;
  129. tmp = info.uptime % 86400;
  130. hours = tmp / 3600;
  131. tmp = tmp % 3600;
  132. minutes = tmp / 60;
  133. tmp = tmp % 60;
  134. snprintf(line2, sizeof(line2), " %03dd %02d:%02d:%02d", days, hours, minutes, tmp);
  135. lcd_setlines(dev, "Uptime:", line2);
  136. retval = 1000;
  137. } else if (subpage == 4) {
  138. double loadavg[3];
  139. if (getloadavg(loadavg, 3) < 0) {
  140. return 0;
  141. }
  142. snprintf(line2, sizeof(line2), " %1.2f %1.2f %1.2f", loadavg[0], loadavg[1], loadavg[2]);
  143. lcd_setlines(dev, "Load Average:", line2);
  144. retval = 1000;
  145. } else {
  146. return LCDPAGE_COMMAND_NEXT;
  147. }
  148. /* update every 1000ms */
  149. return retval;
  150. }
  151. static int lcdpage_network(struct lcddev *dev, int event, void *privdata)
  152. {
  153. static int subpage;
  154. switch (event) {
  155. case LCDPAGE_EVENT_BUTTON1:
  156. lcd_set_backlight(dev, 1);
  157. subpage++;
  158. break;
  159. case LCDPAGE_EVENT_ENTER:
  160. lcd_set_backlight(dev, 1);
  161. subpage = 0;
  162. break;
  163. case LCDPAGE_EVENT_BACKLIGHT:
  164. case LCDPAGE_EVENT_EXIT:
  165. return 0;
  166. default:
  167. break;
  168. }
  169. switch (subpage) {
  170. case 0:
  171. lcd_setlines(dev, "Address(br0):", "10.10.250.135");
  172. break;
  173. case 1:
  174. lcd_setlines(dev, "Netmask(br0):", "255.255.0.0");
  175. break;
  176. case 2:
  177. lcd_setlines(dev, "Gateway(br0):", "10.10.250.250");
  178. break;
  179. default:
  180. return LCDPAGE_COMMAND_NEXT;
  181. }
  182. return 0;
  183. }
  184. void lcdpage_free(void)
  185. {
  186. /* nothing to do, lcdpages are cleaned up in lcd_close() */
  187. }
  188. int lcdpage_init(struct lcddev *dev)
  189. {
  190. lcd_addpage_cb(dev, 200, lcdpage_system, NULL);
  191. lcd_addpage_cb(dev, 100, lcdpage_network, NULL);
  192. return 0;
  193. }