Is there any way to take control over Fluentbit open descriptors? - descriptor

i am trying to fix the next issue:
A lot of file descriptors opened by fluent-bit are kept always opened, even after calling flb_destroy().
Scenario:
I am developing a C++ application that wraps fluent-bit as library, stopping and starting the fluent-bit engine upon received events.
Problem:
Fluent-bit runs out of available file descriptors some time later, after restarting the engine several times without stopping the C++ app, ending with the error trace "lib backend failed".
Details:
I extracted a couple of methods available in the fluent-bit.c source file of the project as a helper on loading the configuration for runtime, by calling the next one:
flb_service_conf("path/to/fluent-bit.conf")
Every time an event is received, flb_stop() and flb_destroy() are called first, doing the same with flb_service_conf() and flb_start() inmediately after.
I got stucked at this point and am still researching about how could fix the issue.
Any clue, idea or suggestion would be very appreciated.
Here is a bit of code, hoping it helps to explain:
bool FluentBit::start( ) {
if ( isStopped() ) {
LOG_DEBUG( ) << "Starting fluent-bit";
m_ctx = ::flb_create( );
if( m_ctx == nullptr ) {
throw std::bad_alloc( );
}
running = configureEngine() && startEngine();
}
return running;
}
void FluentBit::stop( ) {
if ( isRunning() ) {
LOG_DEBUG( ) << "Stopping fluent-bit";
::flb_stop( m_ctx );
while( m_ctx->status == FLB_LIB_OK ) {
std::this_thread::sleep_for( std::chrono::seconds( 1 ) );
}
::flb_destroy( m_ctx );
running = false;
}
}
bool FluentBit::isRunning( ) {
return running;
}
bool FluentBit::isStopped( ) {
return ( !running ) ;
}
bool FluentBit::configureEngine( ) {
auto returnConf = ::flb_service_conf( m_ctx->config, settings.filePath() );
if ( returnConf < 0 ) {
LOG_ERROR() << "Failed loading fluent-bit configuration file: " << settings.filePath();
::flb_destroy( m_ctx );
}
return ( returnConf < 0 ) ? false : true;
}
bool FluentBit::startEngine( ) {
LOG_DEBUG( ) << "Starting fluent-bit engine";
auto retStart = ::flb_start( m_ctx );
if( retStart < 0 ) {
LOG_ERROR( ) << "Could not start fluent-bit engine";
::flb_destroy( m_ctx );
}
running = ( retStart < 0 ) ? false : true;
return running;
}
Thanks in advance for your (possible) replies :)
Kind Regards,
Pablo.

Related

Swift if directive quirk

Suppose I have a complex chain of ifs. Now I want to be sure that one of the conditions are triggered, so I add the final line to notify me when nothing was triggered.
if condition1 ...
else if condition2 ...
else if condition3 ...
else print( "Ooops nothing triggered" )
Since I only want to print this in DEBUG mode, I wrap the final line
#if DEBUG
else print( "Ooops nothing triggered" )
#endif
and I get the strangest errors. "Closure expression is unused" and "Expected expression".
Here is some simple working code to illustrate with.
let x = Int ( arc4random_uniform ( 100 ) ) - 50
// This works
if x > 0 { print ( "Positive" ) }
else if x < 0 { print ( "Negative" ) }
else { print ( "Oops - neither" ) }
// After adding the #if this does not compile
if x > 0 { print ( "Positive" ) }
else if x < 0 { print ( "Negative" ) }
#if DEBUG
else { print ( "Oops - neither" ) }
#endif
Obviously Swift's conditional compilation here fails. I wonder why and ask how to to do this so that the test is only applied in DEBUG mode?
FWIW I am not interested in e.g. using switch - the actual logic is complex. How to do this with ifs and else ifs if possible please?
These #ifs don't really work like those preprocessor directives in C. They
According to the grammar, the syntax of these directives are the following:
if-directive-clause → if-directive compilation-condition statements(opt)
elseif-directive-clauses → elseif-directive-clause elseif-directive-clauses(opt)
elseif-directive-clause → elseif-directive compilation-condition statements(opt)
else-directive-clause → else-directive statements(opt)
if-directive → #if
elseif-directive → #elseif
else-directive → #else
endif-directive → #endif
Note that an if-directive-clause consists of #if, then a compilation-condition, then some statements. Not just anything, but statements.
else { print ( "Oops - neither" ) }
is not a statement (or a list of statements).
How about putting the #if inside the else block? That is guaranteed to be a statements:
if x > 0 { print ( "Positive" ) }
else if x < 0 { print ( "Negative" ) }
else {
#if DEBUG
print ( "Oops - neither" )
#endif
}

Mbedtls entropy generation runs forever

I'm trying to write an test function for mbedtls which randomly generates a key for AES encryption.
I use the original tutorial Code from mbedtls.
My Programm always stops when executing "mbedtls_ctr_drbg_seed()".
About my environmet: Basic Sourcefiles from STM_CUBEmx, Board: ST32F767 Nucleo, Compiling based on Makefile from STM_Cube
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_entropy_context entropy;
char *pers="anything";
int ret;
//Start
mbedtls_entropy_init(&entropy);
debugPrintln("Init entropy done");
mbedtls_ctr_drbg_init(&ctr_drbg);
debugPrintln("Init ctr_drbg done");
if((ret=mbedtls_ctr_drbg_seed(&ctr_drbg,mbedtls_entropy_func,&entropy,(unsigned char *) pers,strlen(pers)))!=0){
//Error info
debugPrintln("ERROR ctr_drbg_seed ");
return -1;
}
debugPrintln("Init ctr_drbg_seed done");
if((ret=mbedtls_ctr_drbg_random(&ctr_drbg,key,32))!=0){
return -1;
}
Thank you in advance
From your description,
I am assuming that your application is stuck in the call to mbedtls_ctr_drbg_seed().
Most probable reason, IMHO, is in the functionmbedtls_entropy_func():
do
{
if( count++ > ENTROPY_MAX_LOOP )
{
ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
goto exit;
}
if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
goto exit;
done = 1;
for( i = 0; i < ctx->source_count; i++ )
if( ctx->source[i].size < ctx->source[i].threshold )
done = 0;
}
while( ! done );
You should check that your entropy collection increases the collected size, that the threshold is not MAX_INT or something of the sort, and that your hw entropy collector actually returns entropy data.
I have found the reason
STM32 Cube MX places the HAL Init function for the RNG after the mbedtls init
So when I call mbedtls_ctr_drbg_seed() inside mbedtls_init() the RNG hasn't yet been initialized and it iterates forever inside:
do
{
if( count++ > ENTROPY_MAX_LOOP )
{
ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
goto exit;
}
if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
goto exit;
done = 1;
for( i = 0; i < ctx->source_count; i++ )
if( ctx->source[i].size < ctx->source[i].threshold )
done = 0;
}
while( ! done );
Solution
swap the lines

check_megaraid_sas nagios plugin explanation

Can anyone explain following piece of code in https://github.com/simondeziel/custom-nagios-plugins/blob/master/plugins/check_megaraid_sas . (line num 220-223)
Why this code is there
} elsif ( $slotnumber != 255 ) {
$pdbad++;
$status = 'CRITICAL';
}
It makes sense to look at the complete section:
PDISKS: while (<PDLIST>) {
if ( m/Slot Number\s*:\s*(\d+)/ ) {
$slotnumber = $1;
$pdcount++;
} elsif ( m/(\w+) Error Count\s*:\s*(\d+)/ ) {
if ( $1 eq 'Media') {
$mediaerrors += $2;
} else {
$othererrors += $2;
}
} elsif ( m/Predictive Failure Count\s*:\s*(\d+)/ ) {
$prederrors += $1;
} elsif ( m/Firmware state\s*:\s*(\w+)/ ) {
$fwstate = $1;
if ( $fwstate eq 'Hotspare' ) {
$hotsparecount++;
} elsif ( $fwstate eq 'Online' ) {
# Do nothing
} elsif ( $fwstate eq 'Unconfigured' ) {
# A drive not in anything, or a non drive device
$pdcount--;
} elsif ( $slotnumber != 255 ) {
$pdbad++;
$status = 'CRITICAL';
}
}
} #PDISKS
That section loops over a list of PDs (Primary Disks?), and I assume that this file / program output contains a human readable status for every attached device. The code looks at every line and performs some actions depending on the content of that line:
$slotnumber is assigned whenever there is Slot Number : ... in the contents of PDLIST. From looking at the logic, if there is a Firmware state line that is not Hotspare, Online or Unconfigured, and the $slotnumber is not 255, then something went horribly wrong and the status is considered CRITICAL. The number of bad PDs ($pdbad) is then increased by one.

Using GetPrivateProfileStringW with UTF-8 encoded files?

Up until now my INI text files have been Unicode encoded and I have been doing things like this to read it:
::GetPrivateProfileStringW(
strUpdateSection,
strTalkKey,
_T(""),
strTalk.GetBuffer( _MAX_PATH ),
_MAX_PATH,
m_strPathINI );
strTalk.ReleaseBuffer();
The file gets downloaded from the internet first and then accessed. But I was finding that for Arabic the text file was getting corrupted. Unless I change the encoding to UTF-8. When I do that, it downloads right. But then it doesn't read the data right from the INI.
So does a INI file have to be Unicode encoded or should it also be OK to use UTF-8 with the function calls?
I think it is about time I convert this part of my program to UTF-8 encoded XML files instead! Way to go.
But wanted to ask the question first.
I should clarify that the text file was initial saved as UTF-8 using NotePad.
Update
This is what it looks like when I read the file:
This is how I download the file:
BOOL CUpdatePublicTalksDlg::DownloadINI()
{
CInternetSession iSession;
CHttpFile *pWebFile = NULL;
CWaitCursor wait;
CFile fileLocal;
TCHAR szError[_MAX_PATH];
int iRead = 0, iBytesRead = 0;
char szBuffer[4096];
BOOL bOK = FALSE;
DWORD dwStatusCode;
CString strError;
// ask user to go online
if( InternetGoOnline( (LPTSTR)(LPCTSTR)m_strURLPathINI, GetSafeHwnd(), 0 ) )
{
TRY
{
// our session should already be open
// try to open up internet session to my URL
// AJT V10.4.0 Use flag INTERNET_FLAG_RELOAD
pWebFile = (CHttpFile*)iSession.OpenURL( m_strURLPathINI, 1,
INTERNET_FLAG_TRANSFER_BINARY |
INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD);
if(pWebFile != NULL)
{
if( pWebFile->QueryInfoStatusCode( dwStatusCode ) )
{
// 20x codes mean success
if( (dwStatusCode / 100) == 2 )
{
// Our downloaded file is only temporary
m_strPathINI = theApp.GetTemporaryFilename();
if( fileLocal.Open( m_strPathINI,
CFile::modeCreate|CFile::modeWrite|CFile::typeBinary ) )
{
iRead = pWebFile->Read( szBuffer, 4096 );
while( iRead > 0 )
{
iBytesRead += iRead;
fileLocal.Write( szBuffer, iRead );
iRead = pWebFile->Read( szBuffer, 4096 );
}
fileLocal.Close();
bOK = TRUE;
}
}
else
{
// There was a problem!
strError.Format( IDS_TPL_INVALID_URL, dwStatusCode );
AfxMessageBox( strError,
MB_OK|MB_ICONERROR );
}
}
}
else
{
AfxMessageBox( ID_STR_UPDATE_CHECK_ERR, MB_OK|MB_ICONERROR );
}
}
CATCH( CException, e )
{
e->GetErrorMessage( szError, _MAX_PATH );
AfxMessageBox( szError, MB_OK|MB_ICONERROR );
}
END_CATCH
// Tidy up
if( pWebFile != NULL )
{
pWebFile->Close();
delete pWebFile;
}
iSession.Close();
}
return bOK;
}
This is how I read the file:
int CUpdatePublicTalksDlg::ReadTalkUpdateINI()
{
int iLastUpdate = 0, iUpdate;
int iNumTalks, iTalk;
NEW_TALK_S sTalk;
CString strUpdateSection, strTalkKey, strTalk;
// How many possible updates are there?
m_iNumUpdates = ::GetPrivateProfileIntW(
_T("TalkUpdates"),
_T("NumberUpdates"),
0,
m_strPathINI );
// What what the last talk update count?
iLastUpdate = theApp.GetLastTalkUpdateCount();
// Loop available updates
for( iUpdate = iLastUpdate + 1; iUpdate <= m_iNumUpdates; iUpdate++ )
{
// Build section key
strUpdateSection.Format( _T("Update%d"), iUpdate );
// How many talks are there?
iNumTalks = ::GetPrivateProfileIntW(
strUpdateSection,
_T("NumberTalks"),
0,
m_strPathINI );
// Loop talks
for( iTalk = 1; iTalk <= iNumTalks; iTalk++ )
{
// Build key
strTalkKey.Format( _T("Talk%d"), iTalk );
// Get talk information
::GetPrivateProfileStringW(
strUpdateSection,
strTalkKey,
_T(""),
strTalk.GetBuffer( _MAX_PATH ),
_MAX_PATH,
m_strPathINI );
strTalk.ReleaseBuffer();
// Decode talk information
DecodeNewTalk( strTalk, sTalk );
// Does it already exists in the database?
// AJT v11.2.0 Bug fix - we want *all* new talks to show
//if( !IsExistingTalk( sTalk.iTalkNumber ) )
//{
//if(!LocateExistingTheme(sTalk, false))
AddNewTalkToListBox( sTalk );
//}
}
}
// Return the actual amount of updates possible
return m_iNumUpdates - iLastUpdate;
}
This is the file being downloaded:
http://publictalksoftware.co.uk/TalkUpdate_Arabic2.ini
Update
It seems that the file is corrupt at the point of being downloaded:
Please see updated Watch:
Making progress, I now confirm the data OK at this point:

Command Line Program - Sending 'Enter' commands

I have command line program that has a menu with multiple choice. I can pass the parameter via program.exe < input.txt but I am having problems sending the 'Enter' command (the program crashes if I attempt to send anything else; such as a ' ' character).
I enter 'All' as Proc name. It then prints 'enter proc name' again and I have to hit Enter for the code to continue. What I cannot work out is how to send this Enter to the program. I have tried a black line in input.txt but that did not work.
while( !end )
{
for( legal=0; !legal; )
{
printf("\tEnter Proc Name: ");
if( fgets( str, 70, stdin ) == NULL )
{
printf("Bye.\n");
exit(0);
}
ret = sscanf(str, "%s", p.name );
if( ret > 0 || str[0] == '\n' ) legal = 1;
else printf("Please enter a legal proc name, none, or all\n");
}
if( str[0] == '\n' ) {
end = 1;
} else if( !strcmp( p.name, "all" )) {
for( i=0; i < Conf_num_procs( &Cn ); i++ )
Status_vector[i] = 1;
} else if( !strcmp( p.name, "none" )) {
for( i=0; i < Conf_num_procs( &Cn ); i++ )
Status_vector[i] = 0;
} else {
proc_index = Conf_proc_by_name( p.name, &p );
if( proc_index != -1 ) {
Status_vector[proc_index] = 1;
} else printf("Please! enter a legal proc name, none, or all\n");
}
}