Online monitoring is essential to enhance the reliability for various systems including cyber-physical systems and Web services. During online monitoring, the system traces are checked against monitoring rules in real time to detect deviations from normal behaviors. In general, the rules are defined as boundary conditions by the experts of the monitored system. This work studies the problem of synthesizing online monitoring rules in the form of temporal logic formulas in an automated way. The monitoring rules are described as past-time signal temporal logic (ptSTL) formulas and an algorithm to synthesize such formulas from a given set of labeled system traces is proposed. The algorithm searches the formula space using genetic algorithms and produces the best formula representing a monitoring rule. In addition, online STL monitoring algorithm is improved to efficiently compute a quantitative valuation for piecewise-constant signals from ptSTL formulas, thus, to reduce the overhead of the real-time computation. The effectiveness of the results is shown on two illustrative examples inspired from online monitoring of Web services.