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.

273 lines
9.6 KiB

​
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "servaKernel.h"
  2. #include <sys/types.h>
  3. #include <sys/sysinfo.h>
  4. #include <sys/statvfs.h>
  5. #define LOG( X ) kernelPrototype::m_pLog->Log( "KRNL", this, X )
  6. #ifdef DEBUG
  7. #define LOG_ERROR( X ) {\
  8. char _tBfrErrMSG234[256];\
  9. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  10. kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, _tBfrErrMSG234 );\
  11. }
  12. #else
  13. #define LOG_ERROR( X ) kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, X );
  14. #endif
  15. bool servaKernel::cleanUpMess(){
  16. LOG("Cleanup");
  17. //Get Not transfered Items
  18. DBC_UINT64_LIST *tList = m_pDBC->listMediaQueueItems( 0 );
  19. if ( tList ) {
  20. LOG("1");
  21. for ( DBC_UINT64_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) {
  22. LOG("*");
  23. DBC_RPC_MEDIA_INFO_EX *tData = m_pDBC->getRemoteMediaInfoEx( *idx );
  24. if ( tData ) {
  25. LOG("2");
  26. CSTR tRB = m_pSM->getReportDir( m_pDBC->getSessionTrackId( tData->session_id ), tData->ts );
  27. if ( tRB ) {
  28. LOG("3");
  29. char tName[MAX_PATH];
  30. sprintf( tName, "%s/%s.%s", tRB, tData->uuid, tData->fmt );
  31. LOG( tName );
  32. free( tRB );
  33. unlink( tName );
  34. };
  35. LOG("f");
  36. free( tData );
  37. };
  38. m_pDBC->removeRemoteMediaInfo( *idx );
  39. };
  40. LOG("-");
  41. tList->clear();
  42. delete( tList );
  43. };
  44. LOG("LVL2");
  45. //Get Not Converted Items
  46. tList = m_pDBC->listMediaQueueItems( 2 );
  47. if ( tList ) {
  48. LOG("1");
  49. for ( DBC_UINT64_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) {
  50. LOG("*");
  51. DBC_RPC_MEDIA_INFO_EX *tData = m_pDBC->getRemoteMediaInfoEx( *idx );
  52. if ( tData ) {
  53. LOG("2");
  54. CSTR tRB = m_pSM->getReportDir( m_pDBC->getSessionTrackId( tData->session_id ), tData->ts );
  55. LOG("2a");
  56. if ( tRB ) {
  57. LOG("3");
  58. char tName[MAX_PATH];
  59. sprintf( tName, "%s/%s.%s", tRB, tData->uuid, "mp4" );
  60. free( tRB );
  61. LOG( tName );
  62. unlink( tName );
  63. };
  64. LOG("f");
  65. free( tData );
  66. };
  67. m_pDBC->updateEncodeStatus( *idx, 1, 0 );
  68. };
  69. tList->clear();
  70. delete( tList );
  71. };
  72. LOG("Close all Staled Sessions");
  73. LOG("Done");
  74. return true;
  75. };
  76. bool servaKernel::registerServA(){
  77. DBC_SERVA_CONFIG *tData = m_pDBC->getServaConfig();
  78. if ( tData ) {
  79. memcpy(&m_sServaConfig, tData, sizeof(DBC_SERVA_CONFIG));
  80. } else {
  81. memset(&m_sServaConfig, 0, sizeof(DBC_SERVA_CONFIG));
  82. m_sServaConfig.encoderLimit = 1;
  83. };
  84. m_pDBC->setServerStatValue( SERVER_VERSION, _BUILD_STRING_ );
  85. m_pDBC->setSystemProp("version_string", _BUILD_STRING_ );
  86. #ifndef NO_EXEC_EXTERNALS
  87. char OS_INFO[120];
  88. FILE *fd = popen("/bin/uname -s -r -v -m","r");
  89. if( fd!= NULL ) {
  90. fgets( OS_INFO, 127, fd);
  91. OS_INFO[127] = (char)0;
  92. trim( OS_INFO );
  93. fclose( fd );
  94. fd = NULL;
  95. m_pDBC->setServerStatValue( SERVER_OS, OS_INFO );
  96. m_pDBC->setSystemProp("os_info", (CSTR) OS_INFO );
  97. } else {
  98. m_pDBC->setServerStatValue( SERVER_OS, "Linux" );
  99. m_pDBC->setSystemProp("os_info", "Linux" );
  100. };
  101. #else
  102. FILE *fd = NULL;
  103. m_pDBC->setServerStatValue( SERVER_OS, "Linux" );
  104. m_pDBC->setSystemProp("os_info", "Linux" );
  105. #endif
  106. char HW_INFO[128];
  107. fd = fopen("/proc/cpuinfo","r");
  108. if ( fd!= NULL ) {
  109. char tBB[1024];
  110. bool bARM = false;
  111. while( !feof( fd ) ) {
  112. fgets( tBB, 1024, fd );
  113. trim( tBB );
  114. if( strcasestr(tBB, "model") != NULL ) {
  115. if( strcasestr(tBB, "model name") != NULL ) {
  116. if ( !bARM ) {
  117. if( strcasestr( tBB, "ARM" ) != NULL){
  118. bARM = true;
  119. } else {
  120. CArray *tA = explode(':', tBB );
  121. if ( tA ) {
  122. strncpy( HW_INFO, tA->at(1), 127 );
  123. delete tA;
  124. };
  125. break;
  126. };
  127. };
  128. } else {
  129. if( bARM ) {
  130. CArray *tA = explode(':', tBB);
  131. if ( tA ) {
  132. strncpy( HW_INFO, tA->at(1), 127 );
  133. delete tA;
  134. };
  135. break;
  136. };
  137. };
  138. };
  139. };
  140. HW_INFO[127] = (char)0;
  141. trim( HW_INFO );
  142. fclose( fd );
  143. fd = NULL;
  144. };
  145. m_pDBC->setSystemProp("hw_info", (CSTR) HW_INFO );
  146. m_pDBC->setServerStatValue( SERVER_HW, (CSTR) HW_INFO );
  147. m_pDBC->setServerStatValue( SERVER_START_TS, (uint32_t) time(NULL) );
  148. return true;
  149. };
  150. bool servaKernel::updateStatus(){
  151. struct sysinfo SI;
  152. struct statvfs DI;
  153. DBC_SYS_LOAD_INFO info = { 0 };
  154. info.CPU_CORES = get_nprocs();
  155. info.C_TS = (uint32_t) time(NULL);
  156. if( sysinfo( &SI ) == 0 ){
  157. float f_load = 100.f / (float)( info.CPU_CORES * ( 1 << SI_LOAD_SHIFT ) );
  158. info.cpu1 = SI.loads[0] * f_load;
  159. info.cpu5 = SI.loads[1] * f_load;
  160. info.cpu15 = SI.loads[2] * f_load;
  161. info.T_MEM = SI.totalram;
  162. info.C_MEM = SI.totalram - SI.freeram;
  163. info.mem = 100.0f * (float)info.C_MEM / (float)info.T_MEM;
  164. } else {
  165. LOG_ERROR("Error while getting System Info!");
  166. };
  167. //const uint32_t MB = 1 << 10;//(1024 * 1024);// * 1024;
  168. if( statvfs("/", &DI) == 0 ){
  169. info.T_DISK = ( DI.f_blocks * (unsigned long long) DI.f_frsize ) >> 10;// / MB;
  170. //cDISK = ( DI.f_bavail * (unsigned long long) DI.f_bsize ) >> 10;// / MB;
  171. info.C_DISK = info.T_DISK - ( ( DI.f_bfree * (unsigned long long) DI.f_bsize ) >> 10 );
  172. info.disk = 100.0f * (float)info.C_DISK / (float)info.T_DISK;
  173. } else {
  174. LOG_ERROR("Error while getting Disk Info!");
  175. };
  176. //Check if need to erase some reports
  177. if ( m_sServaConfig.historyAutoDeleteLimit ) {
  178. #ifdef DEBUG_EXTRA
  179. LOG("History Auto Delete is set!");
  180. #endif
  181. if (m_sServaConfig.historyPreserveFreeSpacePerc || m_sServaConfig.historyPreserveFreeSpaceMB) {
  182. #ifdef DEBUG_EXTRA
  183. LOG("History Size is set!");
  184. #endif
  185. uint64_t tid = 0;
  186. if ( m_sServaConfig.historyPreserveFreeSpacePerc > ( 100.0f - info.disk ) ) {
  187. tid = m_pDBC->getSessionOldest( m_sServaConfig.historyAutoDeleteLimit );
  188. } else if ( m_sServaConfig.historyPreserveFreeSpaceMB > ( info.T_DISK - info.C_DISK ) ) {
  189. tid = m_pDBC->getSessionOldest( m_sServaConfig.historyAutoDeleteLimit );
  190. };
  191. if ( tid ) {
  192. #ifdef DEBUG_EXTRA
  193. LOG("Need to Delete some Sessions!");
  194. #endif
  195. m_pSM->eraseSession( tid );
  196. #ifdef DEBUG_EXTRA
  197. LOG("Delete Session Done!");
  198. #endif
  199. };
  200. #ifdef DEBUG_EXTRA
  201. } else {
  202. LOG("No History Size is set!");
  203. #endif
  204. };
  205. if ( m_sServaConfig.historyDaysLimit ) {
  206. #ifdef DEBUG_EXTRA
  207. LOG("History Time is set!");
  208. #endif
  209. uint32_t cts = time( NULL ) - (m_sServaConfig.historyDaysLimit * 86400);
  210. uint64_t tid = m_pDBC->getSessionOlderThan( cts, m_sServaConfig.historyAutoDeleteLimit );
  211. if ( tid ) {
  212. #ifdef DEBUG_EXTRA
  213. LOG("Need to Delete some Sessions!");
  214. #endif
  215. m_pSM->eraseSession( tid );
  216. #ifdef DEBUG_EXTRA
  217. LOG("Delete Session Done!");
  218. #endif
  219. };
  220. #ifdef DEBUG_EXTRA
  221. } else {
  222. LOG("No History Time Limit is set!");
  223. #endif
  224. };
  225. #ifdef DEBUG
  226. } else {
  227. LOG("No History Auto Delete!");
  228. #endif
  229. };
  230. //Temperature reading
  231. info.temperature = -1;
  232. if( fileExist( "/sys/class/thermal" ) ) {
  233. char tB[128];
  234. // Check first 8 zones to find CPU one
  235. for ( uint8_t i = 0; i < 8; i++ ) {
  236. sprintf( tB, "/sys/class/thermal/thermal_zone%d", i );
  237. if ( fileExist( tB ) ) {
  238. sprintf( tB, "/sys/class/thermal/thermal_zone%d/type", i );
  239. if ( fileExist( tB ) ) {
  240. FILE *fd = fopen( tB, "r" );
  241. readLine( fd, tB, 128 );
  242. fclose(fd);
  243. if ( strcasestr(tB,"cpu")!=NULL || strcasestr(tB,"x86_pkg")!=NULL || strcasestr(tB,"acpitz")!=NULL ) {
  244. sprintf( tB, "/sys/class/thermal/thermal_zone%d/temp", i );
  245. FILE *fd = fopen( tB, "r" );
  246. readLine( fd, tB, 128 );
  247. fclose(fd);
  248. info.temperature = ((float) atoll( tB )) / 1000.0f;
  249. break;
  250. };
  251. } else continue;
  252. } else break;
  253. };
  254. };
  255. return m_pDBC->updateServerUtilisation( &info );
  256. };
  257. #undef LOG
  258. #undef LOG_ERROR