def get_waf_metrics(): path_format = '%szones/%s/firewall/events?per_page=50%s' zone_id = get_zone_id() window_start_time = delorean.now().epoch window_end_time = window_start_time - 60 records = [] next_page_id = '' logging.info('Fetching WAF event data starting at %s, going back 60s' % delorean.epoch(window_start_time).format_datetime()) while next_page_id is not None: url = path_format % (ENDPOINT, zone_id, next_page_id) r = get_data_from_cf(url=url) if 'success' not in r or not r['success']: logging.error('Failed to get information from Cloudflare') for error in r['errors']: logging.error('[%s] %s' % (error['code'], error['message'])) return '' if r['result_info']['next_page_id']: next_id = r['result_info']['next_page_id'] logging.debug('Set next_page_id to %s' % next_id) next_page_id = ('&next_page_id=%s' % next_id) else: next_page_id = None for event in r['result']: occurred_at = event['occurred_at'] occurrence_time = delorean.parse(occurred_at).epoch logging.debug('Occurred at: %s (%s)' % (occurred_at, occurrence_time)) if occurrence_time <= window_end_time: logging.debug('Window end time reached, breaking') next_page_id = None break logging.debug('Adding WAF event') records.append(event) now = delorean.now().epoch logging.info('%d WAF events found (took %g seconds so far)' % (len(records), now - window_start_time)) if now - window_start_time > 55: logging.warn('Too many WAF events, skipping (metrics affected)') next_page_id = None return wafexporter.process(records)
class Post(Model): __tablename__ = "blog_post" id = Column(Integer, primary_key=True) author_id = Column(Integer) content = Column(Text) created_at = Column(DateTime, nullable=False, default=lambda: now().datetime) likers = relationship("BlogPostLike", backref="post")
def parse_query_value(query_str): """ Return value for the query string """ try: query_str = str(query_str).strip("\"' ") if query_str == "now": d = now() else: # Parse datetime string or timestamp try: d = epoch(float(query_str)) except ValueError: d = parse(str(query_str)) except (TypeError, ValueError): d = None return d
def parse_query_value(query_str): """ Return value for the query string """ try: query_str = str(query_str).strip('"\' ') if query_str == 'now': d = now().shift(local_tz) else: # Parse datetime string or timestamp try: d = epoch(float(query_str)).shift(local_tz) except ValueError: d = parse(str(query_str), timezone=local_tz) except (TypeError, ValueError): d = None return d
def _delorean(): return delorean.now()
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. from delorean import now, Delorean, parse from kubernetes import client, config import urllib.request import boto3 import os global ANNOTATION_KEY ANNOTATION_KEY = "happyreaper/last-touch" global current_time current_time = now() global MAX_AGE MAX_AGE = int(os.getenv("MAX_AGE", 60)) * 60 # minutes (convert to sec) global MAX_RESTART MAX_RESTART = int(os.getenv("MAX_RESTART", 20)) global DRYRUN DRYRUN = os.getenv("DRYRUN", False) == "True" or os.getenv("DRYRUN", False) == "true" global AWS_REGION AWS_REGION = urllib.request.urlopen("http://169.254.169.254/latest/meta-data/placement/availability-zone").read()[:-1] def container_info(pod): restart_count = 0 container_creating = False for c in pod.status.container_statuses: restart_count += c.restart_count
def current_datetime(): return now().naive()
def validate_post_date(post: WebElement) -> None: date = post.find_element_by_css_selector("a.date") assert str(now().date.year) in date.text.strip() # oh my...
from delorean import now from django.db import models from django.urls import reverse_lazy created_at_default = now().datetime class Post(models.Model): # xxx = models.TextField(unique=True) title = models.CharField(null=False, default="xxx", max_length=1000) content = models.CharField(null=True, blank=True, max_length=5000) created_at = models.DateTimeField(default=created_at_default) visible = models.BooleanField(default=True) author = models.CharField(max_length=100, null=False, default="test") def get_absolute_url(self): return reverse_lazy("blog:post", kwargs={"pk": self.pk}) def __str__(self): visible = "\N{FIRE}" if self.visible else "\N{SLEEPING SYMBOL}" msg = f'[{self.pk}] "{self.title}" {visible}' return msg class Meta: ordering = ["-created_at", "title", "pk"]