monit 任务 monit.d/

本文详细介绍了如何配置服务监控脚本来检查Nginx和MongoDB的服务状态,包括通过PID文件检查Nginx进程,以及通过匹配进程名检查MongoDB进程。同时,文章提供了启动和重启服务的命令配置,并设置了端口检查机制,确保服务正常运行,若服务失败则发送报警并尝试自动重启。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

check process nginx with pidfile /run/nginx.pid
start program = "/usr/bin/sudo service nginx start" with timeout 30 seconds
restart program = "/usr/bin/sudo service nginx restart"
if failed port 80  type tcp then alert
if failed port 80  type tcp then restart

 

或者

# 匹配进程名
check process mongo MATCHING mongo
# 配置服务启动和重启命令
start program = "/usr/bin/sudo service mongod start"
restart program = "/usr/bin/sudo service mongod restart"
# 如果端口27017无法访问则认为服务失败,发报警邮件并重启服务
if failed port 27017  type tcp then alert
if failed port 27017  type tcp then restart

# 如果在三个周期内重启了3次,则不再监控
# if 3 restarts within 3 cycles then unmonitor

 

转载于:https://my.oschina.net/u/3367404/blog/3037548

/** ****************************************************************************** * File Name : gpio.c * Description : This file provides code for the configuration * of all used GPIO pins. ****************************************************************************** * @attention * * <h2><center>© Copyright (c) 2025 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "gpio.h" /* USER CODE BEGIN 0 */ #include "main.h" /* USER CODE END 0 */ /*----------------------------------------------------------------------------*/ /* Configure GPIO */ /*----------------------------------------------------------------------------*/ /* USER CODE BEGIN 1 */ uint8_t uckey_down,uckey_up,uckey,uckey_old; extern float target1; extern float target2; extern float vref; extern int temp0; extern int a; /* USER CODE END 1 */ /** Configure pins as * Analog * Input * Output * EVENT_OUT * EXTI */ void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(ENON_GPIO_Port, ENON_Pin, GPIO_PIN_RESET); /*Configure GPIO pin : PtPin */ GPIO_InitStruct.Pin = LED2_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(LED2_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pin : PtPin */ GPIO_InitStruct.Pin = LED1_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(LED1_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pins : PC10 PC11 PC12 */ GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /*Configure GPIO pin : PtPin */ GPIO_InitStruct.Pin = ENON_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(ENON_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pin : PtPin */ GPIO_InitStruct.Pin = KEY4_Pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLDOWN; HAL_GPIO_Init(KEY4_GPIO_Port, &GPIO_InitStruct); /* EXTI interrupt init*/ HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0); HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); } /* USER CODE BEGIN 2 */ /*mode 1:长按值变化,松开值变为0 mode 0:按下松开后值变化,并且不变 */ uint8_t key_Scan(uint8_t mode) { if(HAL_GPIO_ReadPin(GPIOA,KEY2_Pin)== 0) uckey=1; if(HAL_GPIO_ReadPin(GPIOA,KEY3_Pin)==0) uckey=2; if(HAL_GPIO_ReadPin(GPIOA,KEY1_Pin)==0) uckey=3; uckey_down=uckey&(uckey^uckey_old); uckey_old=uckey; uint8_t key_val; if(mode==1) { if(HAL_GPIO_ReadPin(GPIOA,KEY2_Pin)== 0) key_val=1; else if(HAL_GPIO_ReadPin(GPIOA,KEY3_Pin)== 0) key_val=2; else if(HAL_GPIO_ReadPin(GPIOA,KEY1_Pin)== 0) key_val=3; else key_val=0; } if(mode==0) key_val=uckey_down; return key_val; } // 按键状态变量 volatile uint32_t key2_press_time = 0; volatile uint32_t key3_press_time = 0; volatile uint32_t key1_press_time = 0; volatile uint8_t key2_flag = 0; volatile uint8_t key3_flag = 0; volatile uint8_t key1_flag = 0; void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { uint32_t current_time = HAL_GetTick(); switch(GPIO_Pin) { case GPIO_PIN_10: // KEY2 if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_10) == 0) { // 按下 key2_press_time = current_time; } else { // 释放 if((current_time - key2_press_time) > 20) { // 消抖 key2_flag = 1; } } break; case GPIO_PIN_11: // KEY3 if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_11) == 0) { key3_press_time = current_time; } else { if((current_time - key3_press_time) > 20) { key3_flag = 1; } } break; case GPIO_PIN_12: // KEY1 if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_12) == 0) { key1_press_time = current_time; } else { if((current_time - key1_press_time) > 20) { key1_flag = 1; } } break; } } /* USER CODE END 2 */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ /* USER CODE BEGIN Header */ /** ****************************************************************************** *------完全开源项目------------------------- *------南阳理工学院新能源实验室------------- *------2020年6月30日--V1.0--------------------- ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "adc.h" #include "dma.h" #include "hrtim.h" #include "usart.h" #include "tim.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "mbrtu.h" #include "mbport.h" #include "key.h" #include "dr_pwm.h" #include "f_monit.h" #include "fir.h" #include "pid.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ float adc1_ture=0, adc2_ture=0, adc3_ture=0; float adc1_ture1=0, adc2_ture1=0, adc3_ture1=0; extern float target1; extern float target2; extern float vref; extern int temp0; float y=0; uint16_t ADC_IN[NUM_CHANNELS][BLOCK_SIZE]; float32_t ADC_Ot[NUM_CHANNELS][BLOCK_SIZE]; int a=0; extern volatile uint8_t key2_flag ; extern volatile uint8_t key3_flag ; extern volatile uint8_t key1_flag ; /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ volatile uint32_t gSysTim01msTicks; //定时器SystemTich中断计数 volatile uint32_t ADC_Value[BuffSize];//--ADC采集8路全16位数据--- /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ void Task_05ms_A(void); void Task_05ms_B(void); void Task_05ms_C(void); void Task_05ms_D(void); /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if(temp0 == 1) { y = PID_system(temp0, adc1_ture); } else { y = PID_system(temp0, adc2_ture); } // if(adc1_ture <= 2.18181) // { //// __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,y); // } // else // { // __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,0); // } } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ static uint16_t l_task_id; /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ //HAL_SetTickFreq(100U);//--10ms时基---- HAL_SYSTICK_Config(SystemCoreClock / (1000U / uwTickFreq));//--0.1ms时基-- /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_DMA_Init(); MX_HRTIM1_Init(); MX_ADC1_Init(); MX_ADC2_Init(); MX_LPUART1_UART_Init(); MX_TIM2_Init(); /* USER CODE BEGIN 2 */ // mb_rtu_init( &mb_port, 0x01, MB_PAR_NONE);//--Modbus初始化,设定从机为1号机,无校验位--- //------------------HRTIM启动PWM输出通道----------------------------------- HAL_NVIC_DisableIRQ(DMA1_Channel1_IRQn);//-----关闭DMA中断----- HAL_ADC_Start(&hadc2);//---先启动ADC2---注意多通道同步Cube必须设置32位字。 HAL_ADCEx_MultiModeStart_DMA(&hadc1, (uint32_t*)&ADC_Value,BuffSize);//--然后启动ADC12---注意读取高低位数据。 HAL_HRTIM_WaveformOutputStart(&hhrtim1, HRTIM_OUTPUT_TB1 | HRTIM_OUTPUT_TB2);//----通道打开--- HAL_HRTIM_WaveformOutputStart(&hhrtim1, HRTIM_OUTPUT_TC1 | HRTIM_OUTPUT_TC2);//----通道打开--- //---开启定时器B和C和D-使用Master完美同步--- HAL_HRTIM_WaveformCountStart_IT(&hhrtim1,HRTIM_TIMERID_MASTER | HRTIM_TIMERID_TIMER_B | HRTIM_TIMERID_TIMER_C | HRTIM_TIMERID_TIMER_D);// HAL_Delay(500); HAL_GPIO_WritePin(ENON_GPIO_Port,ENON_Pin,GPIO_PIN_SET);//--打开后级供电---- gSysTim01msTicks = 0; FreqValue = PWM_Period;//--设置频率初始值--- PhaseValue = PWM_Period/2;//--设置相位初始值-- HAL_TIM_Base_Start_IT(&htim2); // printf("MCU Temperature : %.1f\r\n",((ADC_Value[2]*3300/4096)/2.5+25)); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ // ADC_IN[0][0]=ADC_Value[0] & 0xFFFF; // for(uint8_t ch=0;ch<NUM_CHANNELS;ch++) // { // Fir_Proc(&ADC_IN[ch][0],&ADC_Ot[ch][0],ch); // } // printf("%.3f %.3f %.3f ",ADC_Ot[0][0]/4096.f*3.3f,ADC_Ot[1][0]/4096.f*3.3f,ADC_Ot[2][0]/4096.f*3.3f); // printf("%d",a); // 按键处理 if(key2_flag) { key2_flag = 0; if(temp0 == 1) { target1 = target1 - 0.1f; if(target1 < 1) target1 = 1; } a = 1; printf("KEY2 pressed. Target1: %.1f\n", target1); } if(key3_flag) { key3_flag = 0; if(temp0 == 1) { target1 = target1 + 0.1f; if(target1 > 2) target1 = 2; } a = 2; printf("KEY3 pressed. Target1: %.1f\n", target1); } if(key1_flag) { key1_flag = 0; temp0 = -temp0; a = 3; printf("KEY1 pressed. Mode: %s\n", temp0 == 1 ? "BUCK" : "BOOST"); } // if( gSysTim01msTicks >= 5 ) //每0.5ms运行一次 // { // gSysTim01msTicks = 0; // switch( l_task_id ) // { // case 0x00: //任务A,按键任务 // Task_05ms_A(); // break; // case 0x01: //任务B // Task_05ms_B(); // break; // case 0x02: //任务C // Task_05ms_C(); // break; // case 0x03: //任务D // Task_05ms_D(); // break; // } // l_task_id = l_task_id >= 0x03 ? 0 : l_task_id+1; // } } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; /** Configure the main internal regulator output voltage */ HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST); /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV4; RCC_OscInitStruct.PLL.PLLN = 85; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) { Error_Handler(); } /** Initializes the peripherals clocks */ PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_LPUART1|RCC_PERIPHCLK_ADC12; PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1; PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ /******************************************************************************** * @函数: void Task_05ms_A(void) * @描述: 任务A处理函数 * 0.5ms周期调用 * 2ms中的第一个0.5ms ********************************************************************************/ void Task_05ms_A(void) { } /* ******************************************************************************* * @函数: void Task_05ms_B(void) * @描述: 任务B处理函数 * 0.5ms周期调用 * 2ms中的第二个0.5ms ******************************************************************************* */ void Task_05ms_B(void) { static uint32_t uTaskTimeCount = 0; // mb_rtu_poll( &mb_port ); // modbus协议轮询 uTaskTimeCount++; } /* ******************************************************************************* * @函数: void Task_05ms_C(void) * @描述: 任务C处理函数 * 0.5ms周期调用 * 2ms中的第三个0.5ms *----要特别小心,不要sMasterRegs.MCMP1R超过设定的PWM_Period--- *----要特别小心,尽量不使用timB的reset触发,因为有可能丢脉冲(Master Compare1 变化时,Reset信号可能错过一次)。 ******************************************************************************* */ void Task_05ms_C(void) { static uint32_t uTaskTimeCount = 0; /* 此处添加用户代码,代码执行时间应少于500us */ //--调频:50KHz - 125KHz--调相:0-180度-- gADC_V_IN = (uint16_t)( ADC_Value[0] & 0xFFFF );//---ADC1低16位--- gADC_I_IN = (uint16_t)( ADC_Value[1] & 0xFFFF ); gADC_Freq_HW = (uint16_t)( ADC_Value[2] & 0xFFFF );//---频率调节--- gADC_Chip_Temp = (uint16_t)( ADC_Value[3] & 0xFFFF ); gADC_V_OUT = (uint16_t)( ADC_Value[0] / 0xFFFF );//---ADC2高16位IN2--- gADC_I_OUT = (uint16_t)( ADC_Value[1] / 0xFFFF ); gADC_Phase_HW = (uint16_t)( ADC_Value[2] / 0xFFFF );//---相位调节--- gADC_V_NTC = (uint16_t)( ADC_Value[3] / 0xFFFF ); // FreqValue = PWM_Period + gADC_Freq_HW; dr_pwm_setFreq(FreqValue);//--设置频率 //-----------调相:250KHz----0度-133度---------------- PhaseValue = PWM_Period/2 + 0.6*gADC_Phase_HW; if (PhaseValue < PWM_Period/2) //--确保调相不超出范围 PhaseValue = PWM_Period/2; if (PhaseValue >= FreqValue) PhaseValue = FreqValue; //----------------------------------------------------- dr_pwm_setPhase(PhaseValue); //---设置相位 uTaskTimeCount++; } /* ******************************************************************************* * @函数: void Task_05ms_D(void) * @描述: 任务D处理函数 * 0.5ms周期调用 * 2ms中的第四个0.5ms ******************************************************************************* */ void Task_05ms_D(void) { static uint32_t uTaskTimeCount = 0; f_monit( );//上位机监控 /* 正常输出,闪烁5HZ指示灯 */ if(uTaskTimeCount >= 25) { uTaskTimeCount = 0; HAL_GPIO_TogglePin(LED2_GPIO_Port,LED2_Pin); } uTaskTimeCount++; } /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/为什么a的值还是没改变,好像都没有进去while里的变化a的函数
最新发布
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值